Handler.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Exceptions;
  3. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Validation\ValidationException;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. class Handler extends ExceptionHandler
  8. {
  9. /**
  10. * A list of exception types with their corresponding custom log levels.
  11. *
  12. * @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
  13. */
  14. protected $levels = [
  15. ];
  16. /**
  17. * A list of the exception types that are not reported.
  18. *
  19. * @var array<int, class-string<\Throwable>>
  20. */
  21. protected $dontReport = [
  22. ];
  23. /**
  24. * A list of the inputs that are never flashed to the session on validation exceptions.
  25. *
  26. * @var array<int, string>
  27. */
  28. protected $dontFlash = [
  29. 'current_password',
  30. 'password',
  31. 'password_confirmation',
  32. ];
  33. /**
  34. * Register the exception handling callbacks for the application.
  35. *
  36. * @return void
  37. */
  38. public function register()
  39. {
  40. $this->reportable(function (ClientException $e) {
  41. logger()->warning($e->getMessage(), array_merge(request()->all(), request()->headers->all()));
  42. return false;
  43. });
  44. $this->renderable(function (ClientException $e) {
  45. return response()->json([
  46. 'code' => 400,
  47. "error" => $e->getMessage(),
  48. ], 200);
  49. });
  50. $this->renderable(function (ValidationException $e) {
  51. $msg = Arr::first($e->errors());
  52. return response()->json([
  53. "error" => $msg[0],
  54. "code" => 400,
  55. ]);
  56. });
  57. $this->renderable(function (NotFoundHttpException $e) {
  58. return response()->json([
  59. "error" => "数据不存在",
  60. "code" => 400,
  61. ]);
  62. });
  63. }
  64. }