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

102 lines
2.1 KiB

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