123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app;
- use Throwable;
- use think\Response;
- use think\facade\Log;
- use app\common\util\Result;
- use think\exception\Handle;
- use Firebase\JWT\ExpiredException;
- use think\exception\HttpException;
- use Firebase\JWT\BeforeValidException;
- use think\exception\ValidateException;
- use app\common\exception\CatchException;
- use think\exception\HttpResponseException;
- use Firebase\JWT\SignatureInvalidException;
- use app\common\exception\ParentHttpException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- class ExceptionHandle extends Handle
- {
-
- protected $ignoreReport = [
- HttpException::class,
- HttpResponseException::class,
- ModelNotFoundException::class,
- DataNotFoundException::class,
-
- ];
-
- public function report(Throwable $exception): void
- {
-
- parent::report($exception);
- }
-
- public function render($request, Throwable $e): Response
- {
- $className = get_class($e);
- if (!in_array($className, $this->ignoreReport)) {
- Log::debug("尝试捕获异常[{$e->getCode()}]:" . get_class($e) . " | 提示消息:{$e->getMessage()}" . ($e->getPrevious() ? ' | 父错误:' . get_class($e->getPrevious()) : ''));
- }
-
- if ($e instanceof CatchException) {
-
- return Result::restf($e->getCode(), $e->getMessage());
- }
-
- if ($e instanceof ValidateException) {
- return Result::restf(777, "参数验证错误:\n " . $e->getMessage());
- }
- if ($e instanceof SignatureInvalidException) {
-
- return Result::restf(601, '无效的Jwt签名');
- } elseif ($e instanceof BeforeValidException) {
-
-
- return Result::restf(602, 'Jwt在授权期之前');
- } elseif ($e instanceof ExpiredException) {
-
- return Result::restf(603, 'Jwt授权已过期');
- }
-
- if ($e instanceof HttpException && request()->isAjax()) {
- return response($e->getMessage(), $e->getStatusCode());
- }
- if (!in_array($className, $this->ignoreReport)) {
- Log::error('未捕获异常:' . $e->getTraceAsString());
- }
-
- return parent::render($request, $e);
- }
- }
|