海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Exceptions;
  3. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  4. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  5. use Throwable;
  6. use Illuminate\Validation\ValidationException;
  7. class Handler extends ExceptionHandler
  8. {
  9. /**
  10. * A list of the exception types that are not reported.
  11. *
  12. * @var array
  13. */
  14. protected $dontReport = [
  15. //
  16. ];
  17. /**
  18. * A list of the inputs that are never flashed for validation exceptions.
  19. *
  20. * @var array
  21. */
  22. protected $dontFlash = [
  23. 'current_password',
  24. 'password',
  25. 'password_confirmation',
  26. ];
  27. /**
  28. * Register the exception handling callbacks for the application.
  29. *
  30. * @return void
  31. */
  32. public function register()
  33. {
  34. $this->reportable(function (Throwable $e) {
  35. if ($this->shouldReport($e) && app()->bound('sentry')) {
  36. app('sentry')->captureException($e);
  37. }
  38. });
  39. }
  40. /**
  41. * 报告异常
  42. * @return bool|void
  43. */
  44. public function report(Throwable $e): bool
  45. {
  46. // 判断异常是否需要自定义报告...
  47. return false;
  48. }
  49. public function render($request, Throwable $e)
  50. {
  51. // 表单校验
  52. if ($e instanceof ValidationException) {
  53. $errors = $e->errors();
  54. while (is_array($errors)) {
  55. $errors = current($errors);
  56. }
  57. return response()->json([
  58. 'code' => -1,
  59. 'msg' => $errors,
  60. 'data' => [],
  61. 'status' => 500,
  62. ]);
  63. }
  64. // 404
  65. if ($e instanceof NotFoundHttpException) {
  66. return response()->json([
  67. 'code' => -1,
  68. 'msg' => '404 Not Found',
  69. 'data' => [],
  70. 'status' => 500,
  71. ]);
  72. }
  73. //TypeError && ParseError && Exception
  74. /*if ($e instanceof \TypeError || $e instanceof \ParseError || $e instanceof \Exception) {
  75. return response()->json([
  76. 'code' => -1,
  77. 'msg' => $e->getMessage(),
  78. 'data' => [],
  79. 'status' => 500,
  80. 'file' => $e->getFile(),
  81. 'line' => $e->getLine(),
  82. ]);
  83. }
  84. //Exception
  85. if ($e instanceof \Error) {
  86. return response()->json([
  87. 'code' => -1,
  88. 'msg' => $e->getMessage(),
  89. 'data' => [],
  90. 'status' => 500,
  91. 'file' => $e->getFile(),
  92. 'line' => $e->getLine(),
  93. ]);
  94. }*/
  95. return parent::render($request, $e);
  96. }
  97. }