ExceptionHandle.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace app;
  3. use think\exception\Handle;
  4. use think\exception\HttpResponseException;
  5. use Throwable;
  6. use think\Response;
  7. use think\exception\HttpException;
  8. /**
  9. * 应用异常处理类
  10. */
  11. class ExceptionHandle extends Handle
  12. {
  13. public function render($request, Throwable $e): Response
  14. {
  15. // 调试模式
  16. if ($request->isAjax()) {
  17. if ($e instanceof HttpResponseException) {
  18. return $e->getResponse();
  19. } elseif ($e instanceof HttpException) {
  20. if (env('app_debug')) {
  21. return json(['code' => $e->getStatusCode(), 'msg' => $e->getMessage()], $e->getStatusCode());
  22. }
  23. } else {
  24. if (env('app_debug')) {
  25. return json(['code' => 500, 'msg' => $e->getMessage(), 'trace' => $e->getTrace()], 500);
  26. }
  27. }
  28. return json(['code' => 500, 'msg' => '服务器升级中,请稍后重试~~'], 500);
  29. }
  30. return parent::render($request, $e);
  31. }
  32. }