Handler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Exceptions\ApiException;
  4. use Illuminate\Contracts\Container\Container;
  5. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  6. use App\Exceptions\ParameterException;
  7. use Throwable;
  8. class Handler extends ExceptionHandler
  9. {
  10. /**
  11. * A list of the exception types that are not reported.
  12. *
  13. * @var array
  14. */
  15. protected $dontReport = [
  16. //
  17. ];
  18. /**
  19. * A list of the inputs that are never flashed for validation exceptions.
  20. *
  21. * @var array
  22. */
  23. protected $dontFlash = [
  24. 'current_password',
  25. 'password',
  26. 'password_confirmation',
  27. ];
  28. private function customApiResponse($e): \Illuminate\Http\Response
  29. {
  30. $statusCode = $e->getCode();
  31. $message = $e->getMessage();
  32. session()->put('previous_url', url()->current());
  33. switch ($statusCode) {
  34. case 505:
  35. session()->flush();
  36. $proViewPath = 'views.errors.505';
  37. $view = view()->exists($proViewPath) ? $proViewPath : 'errors.505';
  38. break;
  39. case 506:
  40. $proViewPath = 'views.errors.506';
  41. $view = view()->exists($proViewPath) ? $proViewPath : 'errors.506';
  42. break;
  43. case 503:
  44. session()->flush();
  45. $proViewPath = 'views.errors.503';
  46. $view = view()->exists($proViewPath) ? $proViewPath : 'errors.503';
  47. break;
  48. case 600:
  49. session()->flush();
  50. $proViewPath = 'views.errors.600';
  51. $view = view()->exists($proViewPath) ? $proViewPath : 'errors.600';
  52. break;
  53. default:
  54. session()->flush();
  55. $proViewPath = 'views.errors.etc';
  56. $view = view()->exists($proViewPath) ? $proViewPath : 'errors.etc';
  57. break;
  58. }
  59. return response()->view($view, [
  60. 'message' => $message,
  61. 'status' => $statusCode,
  62. ]);
  63. }
  64. /**
  65. * Register the exception handling callbacks for the application.
  66. *
  67. * @return void
  68. */
  69. public function register()
  70. {
  71. $this->reportable(function (Throwable $e) {
  72. });
  73. $this->renderable(function (ApiException $e, $request) {
  74. return $this->customApiResponse($e);
  75. });
  76. $this->renderable(function (ParameterException $e, $request) {
  77. return response()->view('errors.etc', [
  78. 'message' => 'ParameterException: '.$e->getMessage().
  79. ' 경로에 Parameter 형식에 맞춰서 넣어주세요.',
  80. 'status' => '500',
  81. ]);
  82. });
  83. }
  84. public function render($request, Throwable $exception)
  85. {
  86. if ($this->isHttpException($exception)) {
  87. switch ($exception->getStatusCode()) {
  88. case 404:
  89. $proViewPath = 'views.errors.404';
  90. $view = view()->exists($proViewPath) ? $proViewPath : 'errors.404';
  91. return response()->view($view, [], 404);
  92. }
  93. }
  94. $previous = $exception->getPrevious();
  95. if (empty($previous) || ! $previous instanceof ApiException) {
  96. return parent::render($request, $exception);
  97. }
  98. session()->put('previous_url', url()->current());
  99. $errorCode = $previous->getCode();
  100. $proViewPath = "views.errors.$errorCode";
  101. $view = view()->exists($proViewPath) ? $proViewPath : "errors.$errorCode";
  102. if ($errorCode !== 506) {
  103. session()->flush();
  104. }
  105. return response()->view($view, [], 505);
  106. // return parent::render($request, $exception);
  107. }
  108. }