You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

40 lines
1.1 KiB

<?php
namespace App\Exception\Handler;
use App\Constants\ErrorCode;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Validation\ValidationException;
use Psr\Http\Message\ResponseInterface;
// use Hyperf\Di\Annotation\Inject;
// use Hyperf\HttpServer\Contract\ResponseInterface as Response;
use Throwable;
class ValidationExceptionHandler extends ExceptionHandler
{
// /**
// * @Inject
// * @var Response
// */
// private $reponse;
public function handle(Throwable $throwable, ResponseInterface $response)
{
$this->stopPropagation();
$content = json_encode([
"status" => 'error',
"code" => ErrorCode::PARAMS_INVALID,
"result" => [],
"message" => $throwable->validator->errors()->first()
]);
return $response->withStatus($throwable->status)->withBody(new SwooleStream($content));
}
public function isValid(Throwable $throwable): bool
{
return $throwable instanceof ValidationException;
}
}