123456789101112131415161718192021222324252627282930313233343536373839 |
- <?php
- namespace app;
- use think\exception\Handle;
- use think\exception\HttpResponseException;
- use Throwable;
- use think\Response;
- use think\exception\HttpException;
- /**
- * 应用异常处理类
- */
- class ExceptionHandle extends Handle
- {
- public function render($request, Throwable $e): Response {
- // 调试模式
- if (env('app_debug')){
- // Ajax请求返回JSON
- if ($request->isAjax()) {
- if ($e instanceof HttpResponseException) {
- return $e->getResponse();
- } elseif ($e instanceof HttpException) {
- return json(['code'=>$e->getStatusCode(),'msg'=>$e->getMessage()],$e->getStatusCode());
- } else {
- return json(['code' => 500, 'msg' => $e->getMessage(),'trace'=>$e->getTrace()],500);
- }
- }
- // 非Ajax请求默认处理
- return parent::render($request, $e);
- } else {
- if ($request->isAjax()) {
- return json(['code'=>500,'msg'=>'服务器异常'],500);
- } else {
- return parent::render($request, $e);
- }
- }
- }
- }
|