Handler.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Exceptions\ApiException;
  4. use Illuminate\Auth\Access\AuthorizationException;
  5. use Illuminate\Database\Eloquent\ModelNotFoundException;
  6. use Illuminate\Http\Response;
  7. use Illuminate\Validation\ValidationException;
  8. use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
  9. use Symfony\Component\HttpKernel\Exception\HttpException;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Throwable;
  12. class Handler extends ExceptionHandler
  13. {
  14. /**
  15. * A list of the exception types that should not be reported.
  16. *
  17. * @var array
  18. */
  19. protected $dontReport = [
  20. AuthorizationException::class,
  21. HttpException::class,
  22. ModelNotFoundException::class,
  23. ValidationException::class,
  24. ];
  25. /**
  26. * Report or log an exception.
  27. *
  28. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  29. *
  30. * @param \Throwable $exception
  31. * @return void
  32. *
  33. * @throws \Exception
  34. */
  35. public function report(Throwable $exception)
  36. {
  37. parent::report($exception);
  38. }
  39. /**
  40. * Render an exception into an HTTP response.
  41. *
  42. * @param \Illuminate\Http\Request $request
  43. * @param \Throwable $e
  44. * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  45. *
  46. * @throws \Throwable
  47. */
  48. public function render($request, Throwable $e)
  49. {
  50. $content = [
  51. 'code' => $e->getCode() == 0 ? '500' : $e->getCode(),
  52. 'message' => $e->getMessage(),
  53. 'data'=> []
  54. ];
  55. if ($e instanceof HttpResponseException) {
  56. $content['data'] = $e->getResponse();
  57. } elseif ($e instanceof ModelNotFoundException || $e instanceof NotFoundHttpException) {
  58. $content['code'] = 404;
  59. $content['message'] = '您访问的页面地址不存在';
  60. return redirect('/');
  61. } elseif ($e instanceof AuthorizationException) {
  62. $content['code'] = 403;
  63. } elseif ($e instanceof ValidationException && $e->getResponse()) {
  64. $content['code'] = 422;
  65. // $content['message'] = '验证不通过';
  66. $content['data'] = json_decode($e->getResponse()->getContent(), true);
  67. //处理多语言中的[11000,'xxx']格式
  68. $msg = [];
  69. $ret = [];
  70. foreach ($content['data'] as &$item) {
  71. if (is_array($item)) {
  72. foreach ($item as &$item2) {
  73. if (is_array($item2) && count($item2) == 2) {
  74. $msg = array_merge($msg, [$item2[1]]);
  75. $ret = array_merge($ret, [$item2[0]]);
  76. $item2 = $item2[1];
  77. } else {
  78. $msg = array_merge($msg, [$item2]);
  79. }
  80. }
  81. } else {
  82. $msg[] = $item;
  83. }
  84. }
  85. $content['message'] = reset($msg);
  86. $content['code'] = reset($ret) ?: 422;
  87. if (is_array($content['message'])) {
  88. if (count($content['message']) == 2) {
  89. $content['code'] = $content['message'][0];
  90. $content['message'] = $content['message'][1];
  91. }
  92. }
  93. } elseif($e instanceof ApiException){
  94. $content['data'] = $e->getData();
  95. }
  96. $trace = $e->getTraceAsString();
  97. if (isset($content['message']) && $content['message']) {
  98. $track = json_encode($trace, JSON_UNESCAPED_UNICODE);
  99. if (stristr($content['message'], 'connection')
  100. || stristr($content['message'], 'SQLSTATE')
  101. || stristr($track, 'PDOException: SQLSTATE')
  102. ) {
  103. $content['code'] = -100;
  104. //发短信通知
  105. // errorHandleSms($content['message']);
  106. }
  107. }
  108. if (config('app.app_debug')) { // 开发模式增加track输出
  109. $content['track'] = $trace;
  110. }
  111. $response = new Response(json_encode($content));
  112. $response->header('Content-Type', 'application/json;charset:UTF-8');
  113. $response->header('Access-Control-Allow-Origin', '*');
  114. $response->header('Access-Control-Allow-Credentials', 'true');
  115. $response->header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  116. $response->header('Access-Control-Allow-Headers', 'Content-Type,token,api_token');
  117. $response->header('Access-Control-Expose-Headers', '*');
  118. $response->exception = $e;
  119. return $response;
  120. // return parent::render($request, $e);
  121. }
  122. }