海南旅游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.

103 lines
2.1 KiB

4 years ago
4 years ago
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. //
  35. }
  36. /**
  37. * 报告异常
  38. * @return bool|void
  39. */
  40. public function report(Throwable $e): bool
  41. {
  42. if ($this->shouldReport($e) && app()->bound('sentry')) {
  43. app('sentry')->captureException($e);
  44. }
  45. // 判断异常是否需要自定义报告...
  46. return false;
  47. }
  48. public function render($request, Throwable $e)
  49. {
  50. // 表单校验
  51. if ($e instanceof ValidationException) {
  52. $errors = $e->errors();
  53. while (is_array($errors)) {
  54. $errors = current($errors);
  55. }
  56. return response()->json([
  57. 'code' => -1,
  58. 'msg' => $errors,
  59. 'data' => [],
  60. 'status' => 500,
  61. ]);
  62. }
  63. // 404
  64. if ($e instanceof NotFoundHttpException) {
  65. return response()->json([
  66. 'code' => -1,
  67. 'msg' => '404 Not Found',
  68. 'data' => [],
  69. 'status' => 500,
  70. ]);
  71. }
  72. //TypeError && ParseError && Exception
  73. /*if ($e instanceof \TypeError || $e instanceof \ParseError || $e instanceof \Exception) {
  74. return response()->json([
  75. 'code' => -1,
  76. 'msg' => $e->getMessage(),
  77. 'data' => [],
  78. 'status' => 500,
  79. 'file' => $e->getFile(),
  80. 'line' => $e->getLine(),
  81. ]);
  82. }
  83. //Exception
  84. if ($e instanceof \Error) {
  85. return response()->json([
  86. 'code' => -1,
  87. 'msg' => $e->getMessage(),
  88. 'data' => [],
  89. 'status' => 500,
  90. 'file' => $e->getFile(),
  91. 'line' => $e->getLine(),
  92. ]);
  93. }*/
  94. return parent::render($request, $e);
  95. }
  96. }