* @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 Builder\Exception\NormalStatusException; use Psr\Http\Message\ResponseInterface; use Throwable; /** * Class DataNotFoundExceptionHandler * @package Builder\Exception\Handler */ class NormalStatusExceptionHandler extends ExceptionHandler { /** * @param Throwable $throwable * @param ResponseInterface $response * @return ResponseInterface */ public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface { $this->stopPropagation(); $format = [ 'success' => false, 'message' => $throwable->getMessage(), ]; if ($throwable->getCode() != 200 && $throwable->getCode() != 0) { $format['code'] = $throwable->getCode(); } // logger('Exception log')->debug($throwable->getMessage()); return $response->withHeader('Server', 'MineAdmin') ->withAddedHeader('content-type', 'application/json; charset=utf-8') ->withBody(new SwooleStream(Json::encode($format))); } public function isValid(Throwable $throwable): bool { return $throwable instanceof NormalStatusException; } }