Handler.php 4.5 KB

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