* @Link https://gitee.com/xmo/MineAdmin */ declare(strict_types=1); namespace Builder\Exception\Handler; use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\Utils\Codec\Json; use Hyperf\Validation\ValidationException; use Builder\Helper\MineCode; use Psr\Http\Message\ResponseInterface; use Throwable; class ValidationExceptionHandler extends ExceptionHandler { public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface { $this->stopPropagation(); /** @var \Hyperf\Validation\ValidationException $throwable */ $body = $throwable->validator->errors()->first(); $format = [ 'success' => false, 'message' => $body, 'code' => MineCode::VALIDATE_FAILED, ]; return $response->withHeader('Server', 'MineAdmin') ->withAddedHeader('content-type', 'application/json; charset=utf-8') ->withStatus(200)->withBody(new SwooleStream(Json::encode($format))); } public function isValid(Throwable $throwable): bool { return $throwable instanceof ValidationException; } }