123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace app;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\db\exception\PDOException;
- use think\exception\ErrorException;
- use think\exception\Handle;
- use think\exception\HttpException;
- use think\exception\HttpResponseException;
- use think\exception\ValidateException;
- use think\Response;
- use Throwable;
- class ExceptionHandle extends Handle
- {
-
- protected $ignoreReport = [
- HttpException::class,
- HttpResponseException::class,
- ModelNotFoundException::class,
- DataNotFoundException::class,
- ValidateException::class,
- ];
-
- public function report(Throwable $exception): void
- {
-
- parent::report($exception);
- }
-
- public function render($request, Throwable $e): Response
- {
-
- $this->report($e);
-
- if ($e instanceof ValidateException)
- return app('json')->fail($e->getMessage());
- else if ($e instanceof DataNotFoundException)
- return app('json')->fail(isDebug() ? $e->getMessage() : '数据不存在');
- else if ($e instanceof ModelNotFoundException)
- return app('json')->fail(isDebug() ? $e->getMessage() : '数据不存在');
- else if ($e instanceof PDOException)
- return app('json')->fail(isDebug() ? $e->getMessage() : '数据库操作失败', isDebug() ? $e->getData() : []);
- else if ($e instanceof ErrorException)
- return app('json')->fail(isDebug() ? $e->getMessage() : '系统错误', isDebug() ? $e->getData() : []);
- else if ($e instanceof \PDOException)
- return app('json')->fail(isDebug() ? $e->getMessage() : '数据库连接失败');
- else if ($e instanceof \EasyWeChat\Core\Exceptions\HttpException)
- return app('json')->fail($e->getMessage());
- return parent::render($request, $e);
- }
- }
|