Handler.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. class Handler extends ExceptionHandler
  7. {
  8. /**
  9. * A list of exception types with their corresponding custom log levels.
  10. *
  11. * @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
  12. */
  13. protected $levels = [
  14. ];
  15. /**
  16. * A list of the exception types that are not reported.
  17. *
  18. * @var array<int, class-string<\Throwable>>
  19. */
  20. protected $dontReport = [
  21. ];
  22. /**
  23. * A list of the inputs that are never flashed to the session on validation exceptions.
  24. *
  25. * @var array<int, string>
  26. */
  27. protected $dontFlash = [
  28. 'current_password',
  29. 'password',
  30. 'password_confirmation',
  31. ];
  32. /**
  33. * Register the exception handling callbacks for the application.
  34. *
  35. * @return void
  36. */
  37. public function register()
  38. {
  39. $this->reportable(function (ClientException $e) {
  40. logger()->warning($e->getMessage(), array_merge(request()->all(), request()->headers->all()));
  41. return false;
  42. });
  43. $this->renderable(function (ClientException $e) {
  44. return response()->json([
  45. "error" => $e->getMessage(),
  46. ]);
  47. });
  48. $this->renderable(function (\Exception $e) {
  49. return response()->json([
  50. "error" => $e->getMessage(),
  51. "file" => $e->getFile(),
  52. "line" => $e->getLine(),
  53. "trace" => $e->getTrace(),
  54. ]);
  55. });
  56. $this->renderable(function (ValidationException $e) {
  57. $msg = Arr::first($e->errors());
  58. return response()->json([
  59. "error" => $msg[0],
  60. ], 400);
  61. });
  62. }
  63. }