12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace app\adminapi;
- use crmeb\exceptions\AdminException;
- use crmeb\exceptions\AuthException;
- use crmeb\exceptions\UtilException;
- use think\db\exception\DbException;
- use think\exception\Handle;
- use think\exception\ValidateException;
- use think\facade\Env;
- use think\Response;
- use Throwable;
- class AdminApiExceptionHandle extends Handle
- {
- /**
- * 记录异常信息(包括日志或者其它方式记录)
- *
- * @access public
- * @param Throwable $exception
- * @return void
- */
- public function report(Throwable $exception): void
- {
- // 使用内置的方式记录异常日志
- parent::report($exception);
- }
- /**
- * Render an exception into an HTTP response.
- *
- * @access public
- * @param \think\Request $request
- * @param Throwable $e
- * @return Response
- */
- public function render($request, Throwable $e): Response
- {
- $massageData = Env::get('app_debug', false) ? [
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- 'trace' => $e->getTrace(),
- 'previous' => $e->getPrevious(),
- ] : [];
- // 添加自定义异常处理机制
- if ($e instanceof DbException) {
- return app('json')->fail('数据获取失败', $massageData);
- } elseif ($e instanceof AuthException || $e instanceof ValidateException || $e instanceof UtilException) {
- return app('json')->make($e->getCode() ?: 400, $e->getMessage());
- } elseif ($e instanceof AdminException) {
- return app('json')->fail($e->getMessage(), $massageData);
- } else {
- return app('json')->code(200)->make(400, $e->getMessage(), $massageData);
- }
- }
- }
|