44
55use Exception ;
66use Illuminate \Auth \AuthenticationException ;
7+ use Illuminate \Validation \ValidationException ;
8+ use Illuminate \Database \Eloquent \ModelNotFoundException ;
79use Illuminate \Foundation \Exceptions \Handler as ExceptionHandler ;
10+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
11+ use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
812
13+ /**
14+ * Class Handler
15+ * @package App\Exceptions
16+ */
917class Handler extends ExceptionHandler
1018{
1119 /**
@@ -44,6 +52,15 @@ public function report(Exception $exception)
4452 */
4553 public function render ($ request , Exception $ exception )
4654 {
55+ if ($ exception instanceof NotFoundHttpException){
56+ return $ this ->renderNotFoundException ($ exception );
57+ }
58+ if ($ exception instanceof ModelNotFoundException) {
59+ return $ this ->renderModelNotFoundException ($ exception );
60+ }
61+ if ($ exception instanceof AccessDeniedHttpException) {
62+ return $ this ->renderAccessDeniedException ($ exception );
63+ }
4764 return parent ::render ($ request , $ exception );
4865 }
4966
@@ -57,9 +74,71 @@ public function render($request, Exception $exception)
5774 protected function unauthenticated ($ request , AuthenticationException $ exception )
5875 {
5976 if ($ request ->expectsJson ()) {
60- return response ()->json (['error ' => 'Unauthenticated. ' ], 401 );
77+ return response ()->json (['message ' => 'Unauthenticated. ' ], 401 );
6178 }
6279
6380 return redirect ()->guest ('login ' );
6481 }
82+
83+ /**
84+ * Create a response object from the given validation exception.
85+ *
86+ * @param \Illuminate\Validation\ValidationException $e
87+ * @param \Illuminate\Http\Request $request
88+ * @return \Symfony\Component\HttpFoundation\Response
89+ */
90+ protected function convertValidationExceptionToResponse (ValidationException $ e , $ request )
91+ {
92+ if ($ e ->response ) {
93+ return $ e ->response ;
94+ }
95+
96+ $ errors = $ e ->validator ->errors ()->getMessages ();
97+
98+ if ($ request ->expectsJson ()) {
99+ return response ()->json ([
100+ 'message ' => 'Validation error ' ,
101+ 'errors ' => $ errors
102+ ], 422 );
103+ }
104+
105+ return redirect ()->back ()->withInput (
106+ $ request ->input ()
107+ )->withErrors ($ errors );
108+ }
109+
110+ /**
111+ * @return \Illuminate\Http\JsonResponse
112+ */
113+ protected function renderNotFoundException ($ e )
114+ {
115+ if (request ()->expectsJson ()){
116+ return response ()->json (['message ' => 'Not Found ' ], 404 );
117+ }
118+ return $ this ->renderHttpException ($ e );
119+ }
120+
121+ /**
122+ * @param $e
123+ * @return \Illuminate\Http\JsonResponse
124+ */
125+ protected function renderModelNotFoundException ($ e )
126+ {
127+ if (request ()->expectsJson ()){
128+ return response ()->json (['message ' => 'Not Found ' ], 404 );
129+ }
130+ throw new NotFoundHttpException ();
131+ }
132+
133+ /**
134+ * @param $e
135+ * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
136+ */
137+ protected function renderAccessDeniedException ($ e )
138+ {
139+ if (request ()->expectsJson ()){
140+ return response ()->json (['message ' => 'Forbidden ' ], 403 );
141+ }
142+ return $ this ->renderHttpException ($ e );
143+ }
65144}
0 commit comments