Handler.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  5. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  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. //
  15. ];
  16. /**
  17. * A list of the inputs that are never flashed for validation exceptions.
  18. *
  19. * @var array
  20. */
  21. protected $dontFlash = [
  22. 'password',
  23. 'password_confirmation',
  24. ];
  25. /**
  26. * Report or log an exception.
  27. *
  28. * @param \Exception $exception
  29. * @return void
  30. *
  31. * @throws \Exception
  32. */
  33. public function report(Exception $exception)
  34. {
  35. parent::report($exception);
  36. }
  37. /**
  38. * Render an exception into an HTTP response.
  39. *
  40. * @param \Illuminate\Http\Request $request
  41. * @param \Exception $exception
  42. * @return \Symfony\Component\HttpFoundation\Response
  43. *
  44. * @throws \Exception
  45. */
  46. public function render($request, Exception $exception)
  47. {
  48. if ($this->shouldntReport($exception)) {
  49. // 因为laravel-admin利用flush返回错误,必须先这样排除.
  50. return parent::render($request, $exception);
  51. }
  52. //匹配获取新的抛出实例
  53. $e = FlattenException::create($exception);
  54. $statusCode = $e->getStatusCode();
  55. $isDebug = config('app.debug');
  56. $code = 500; //输出的错误代码
  57. $msg404 = array('code' => $statusCode, 'message' => 'Nothing to do!', 'data' => [], 'handshake' => '');
  58. if ($isDebug === false) { //生产环境下
  59. if ($request->wantsJson() || $request->isJson() || $request->ajax()) {
  60. return response()->json($msg404, $statusCode);
  61. } else {
  62. if (view()->exists('errors.' . $statusCode)) {
  63. return response()->view('errors.' . $statusCode, [], $statusCode);
  64. }
  65. }
  66. return response()->view('errors.404', [], $statusCode);
  67. }
  68. return parent::render($request, $exception);
  69. }
  70. }