Handler.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. 'code' => 400,
  46. "error" => $e->getMessage(),
  47. ], 200);
  48. });
  49. $this->renderable(function (ValidationException $e) {
  50. $msg = Arr::first($e->errors());
  51. return response()->json([
  52. "error" => $msg[0],
  53. "code" => 400,
  54. ]);
  55. });
  56. }
  57. }