Handler.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Exceptions;
  3. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  4. use Throwable;
  5. use League\OAuth2\Server\Exception\OAuthServerException;
  6. class Handler extends ExceptionHandler
  7. {
  8. /**
  9. * A list of the exception types that are not reported.
  10. *
  11. * @var array
  12. */
  13. protected $dontReport = [
  14. OAuthServerException::class,
  15. \Zttp\ConnectionException::class,
  16. \GuzzleHttp\Exception\ConnectException::class,
  17. \Illuminate\Http\Client\ConnectionException::class
  18. ];
  19. /**
  20. * A list of the inputs that are never flashed for validation exceptions.
  21. *
  22. * @var array
  23. */
  24. protected $dontFlash = [
  25. 'password',
  26. 'password_confirmation',
  27. ];
  28. /**
  29. * Report or log an exception.
  30. *
  31. * @param \Exception $exception
  32. *
  33. * @return void
  34. */
  35. public function report(Throwable $exception)
  36. {
  37. parent::report($exception);
  38. }
  39. /**
  40. * Register the exception handling callbacks for the application.
  41. *
  42. * @return void
  43. */
  44. public function register()
  45. {
  46. $this->reportable(function (\BadMethodCallException $e) {
  47. return app()->environment() !== 'production';
  48. });
  49. $this->reportable(function (\Illuminate\Http\Client\ConnectionException $e) {
  50. return app()->environment() !== 'production';
  51. });
  52. }
  53. /**
  54. * Render an exception into an HTTP response.
  55. *
  56. * @param \Illuminate\Http\Request $request
  57. * @param \Exception $exception
  58. *
  59. * @return \Illuminate\Http\Response
  60. */
  61. public function render($request, Throwable $exception)
  62. {
  63. if ($exception instanceof \Illuminate\Validation\ValidationException && $request->wantsJson()) {
  64. return response()->json(
  65. [
  66. 'message' => $exception->getMessage(),
  67. 'errors' => $exception->validator->getMessageBag()
  68. ],
  69. method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500
  70. );
  71. } else if ($request->wantsJson()) {
  72. return response()->json(
  73. ['error' => $exception->getMessage()],
  74. method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500
  75. );
  76. }
  77. return parent::render($request, $exception);
  78. }
  79. }