Handler.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  5. use Illuminate\Http\Response;
  6. use Symfony\Component\HttpKernel\Exception\HttpException;
  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. 'password',
  24. 'password_confirmation',
  25. ];
  26. /**
  27. * Report or log an exception.
  28. *
  29. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  30. *
  31. * @param \Exception $exception
  32. * @return void
  33. */
  34. public function report(Exception $exception)
  35. {
  36. parent::report($exception);
  37. }
  38. /**
  39. * Render an exception into an HTTP response.
  40. *
  41. * @param \Illuminate\Http\Request $request
  42. * @param \Exception $exception
  43. * @return \Illuminate\Http\Response
  44. */
  45. public function render($request, Exception $exception)
  46. {
  47. // 处理 api 异常
  48. if ($request->ajax() && $exception instanceof HttpException) {
  49. $message = $exception->getMessage();
  50. $status_code = $exception->getStatusCode();
  51. return new Response(compact('message','status_code'),$status_code);
  52. }
  53. return parent::render($request, $exception);
  54. }
  55. }