ExceptionHandle.php 3.4 KB

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