Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ExceptionHandle.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app;
  3. use crmeb\exceptions\AdminException;
  4. use crmeb\exceptions\ApiException;
  5. use crmeb\exceptions\AuthException;
  6. use crmeb\exceptions\PayException;
  7. use crmeb\exceptions\TemplateException;
  8. use crmeb\exceptions\UploadException;
  9. use crmeb\exceptions\WechatReplyException;
  10. use crmeb\services\wechat\WechatException;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\ModelNotFoundException;
  13. use think\exception\Handle;
  14. use think\exception\HttpException;
  15. use think\exception\HttpResponseException;
  16. use think\exception\ValidateException;
  17. use think\facade\Env;
  18. use think\Response;
  19. use Throwable;
  20. /**
  21. * 应用异常处理类
  22. */
  23. class ExceptionHandle extends Handle
  24. {
  25. /**
  26. * 不需要记录信息(日志)的异常类列表
  27. * @var array
  28. */
  29. protected $ignoreReport = [
  30. HttpException::class,
  31. HttpResponseException::class,
  32. ModelNotFoundException::class,
  33. DataNotFoundException::class,
  34. ValidateException::class,
  35. AdminException::class,
  36. UploadException::class,
  37. PayException::class,
  38. ApiException::class,
  39. TemplateException::class,
  40. AuthException::class
  41. ];
  42. /**
  43. * 记录异常信息(包括日志或者其它方式记录)
  44. *
  45. * @access public
  46. * @param Throwable $exception
  47. * @return void
  48. */
  49. public function report(Throwable $exception): void
  50. {
  51. // 使用内置的方式记录异常日志
  52. parent::report($exception);
  53. }
  54. /**
  55. * Render an exception into an HTTP response.
  56. *
  57. * @access public
  58. * @param \think\Request $request
  59. * @param Throwable $e
  60. * @return Response
  61. */
  62. public function render($request, Throwable $e): Response
  63. {
  64. // 添加自定义异常处理机制
  65. $massageData = Env::get('app_debug', false) ? [
  66. 'file' => $e->getFile(),
  67. 'line' => $e->getLine(),
  68. 'trace' => $e->getTrace(),
  69. 'previous' => $e->getPrevious(),
  70. ] : [];
  71. // 添加自定义异常处理机制
  72. if ($e instanceof DbException) {
  73. return app('json')->fail('数据获取失败', $massageData);
  74. } elseif ($e instanceof AuthException ||
  75. $e instanceof ValidateException ||
  76. $e instanceof ApiException ||
  77. $e instanceof PayException ||
  78. $e instanceof TemplateException ||
  79. $e instanceof UploadException ||
  80. $e instanceof WechatReplyException ||
  81. $e instanceof AdminException ||
  82. $e instanceof WechatException
  83. ) {
  84. return app('json')->make($e->getCode() ?: 400, $e->getMessage(), $massageData);
  85. } else {
  86. return app('json')->code(500)->make(400, $e->getMessage(), $massageData);
  87. }
  88. }
  89. }