<?php // +---------------------------------------------------------------------- // | [ 后台登录认证异常抛出 ] // +---------------------------------------------------------------------- // | Copyright (c) 2018-2020 rights reserved. // +---------------------------------------------------------------------- // | Author: TABLE ME // +---------------------------------------------------------------------- // | Date: 2020-08-25 16:10 // +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\exception; use library\exceptions\AuthException; use library\exceptions\GeneralException; use think\db\exception\DbException; use think\exception\Handle; use think\Response; use Throwable; class ExecptionHandle 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('app_debug', false) ? [ 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTrace(), 'previous' => $e->getPrevious(), ] : []; if ($e instanceof DbException) { //记录服务器日志 return app('json')->fail('抱歉,服务器数据异常!', ['line'=>$e->getMessage()]); }elseif ($e instanceof AuthException){ return app('json')->make($e->getCode() ?: -1, $e->getMessage()); } elseif ($e instanceof GeneralException) { return app('json')->make(-1, $e->getMessage()); } else { return app('json')->make(-1, $e->getMessage()); } } }