89 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			89 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
| <?php
 | |
| 
 | |
| namespace app;
 | |
| 
 | |
| use app\config\BusinessCode;
 | |
| use app\config\ResponseCode;
 | |
| use app\exception\BusinessException;
 | |
| use app\exception\LogicException;
 | |
| use think\db\exception\DataNotFoundException;
 | |
| use think\db\exception\ModelNotFoundException;
 | |
| use think\exception\Handle;
 | |
| use think\exception\HttpException;
 | |
| use think\exception\HttpResponseException;
 | |
| use think\exception\ValidateException;
 | |
| use think\facade\Log;
 | |
| use think\Response;
 | |
| use Throwable;
 | |
| 
 | |
| /**
 | |
|  * 应用异常处理类
 | |
|  */
 | |
| class ExceptionHandle extends Handle
 | |
| {
 | |
|     /**
 | |
|      * 不需要记录信息(日志)的异常类列表
 | |
|      * @var array
 | |
|      */
 | |
|     protected $ignoreReport = [
 | |
|         HttpException::class,
 | |
|         HttpResponseException::class,
 | |
|         ModelNotFoundException::class,
 | |
|         DataNotFoundException::class,
 | |
|         ValidateException::class,
 | |
|         BusinessException::class,
 | |
|         LogicException::class
 | |
|     ];
 | |
| 
 | |
|     /**
 | |
|      * 记录异常信息(包括日志或者其它方式记录)
 | |
|      *
 | |
|      * @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
 | |
|     {
 | |
| //        if (env('app_debug', false)) {
 | |
| //            return parent::render($request, $e);
 | |
| //        }
 | |
|         Log::error($e->getMessage());
 | |
|         // 添加自定义异常处理机制
 | |
|         if ($e instanceof BusinessException) {
 | |
|             return json_response(['code' => !empty($e->getCode()) ? $e->getCode() : \app\config\BusinessCode::FAIL, 'message' => $e->getMessage()]);
 | |
|         }
 | |
| 
 | |
|         if ($e instanceof ValidateException) {
 | |
|             return json_response(['code' => \app\config\BusinessCode::PARAMETER_ERROR, 'message' => $e->getMessage()]);
 | |
|         }
 | |
|         // 逻辑代码错误 业务失败抛出的异常
 | |
|         if ($e instanceof LogicException) {
 | |
|             if (app()->isDebug()) {
 | |
|                 return response_json($e->getData(), ResponseCode::FAIL, $e->getMessage());
 | |
|             } else {
 | |
|                 return response_json([], ResponseCode::FAIL, $e->getMessage())->header(['error' => 'logic']);
 | |
|             }
 | |
|         }
 | |
|         if ($e instanceof \Exception) {
 | |
|             return json_response(['code' => !empty($e->getCode()) ? $e->getCode() : \app\config\BusinessCode::FAIL, 'message' => $e->getMessage()]);
 | |
|         }
 | |
| 
 | |
|         // 其他错误交给系统处理
 | |
|         return parent::render($request, $e);
 | |
|     }
 | |
| }
 |