mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-03-27 07:00:17 +08:00
39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Exception\Handler;
|
|
|
|
use App\Constants\ResultCode;
|
|
use App\Exception\ErrException;
|
|
use Hyperf\Validation\ValidationException;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Throwable;
|
|
|
|
class ValidationExceptionHandler extends BaseErrExceptionHandler
|
|
{
|
|
/**
|
|
* @param Throwable $throwable
|
|
* @param ResponseInterface $response
|
|
* @return ResponseInterface
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
|
{
|
|
if ($throwable instanceof ValidationException) {
|
|
$throwable = new ErrException($throwable->validator->errors()->first(),ResultCode::ERROR);
|
|
return $this->handlerResponse($throwable,$response);
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param Throwable $throwable
|
|
* @return bool
|
|
*/
|
|
public function isValid(Throwable $throwable): bool
|
|
{
|
|
return $throwable instanceof ValidationException;
|
|
}
|
|
} |