7
7
use Illuminate \Http \Response ;
8
8
use Illuminate \Validation \ValidationException ;
9
9
use KamranAhmed \Faulty \Exceptions \BaseException ;
10
+ use KamranAhmed \Faulty \Exceptions \NotFoundException ;
10
11
use Laravel \Lumen \Exceptions \Handler as LumenExceptionHandler ;
11
12
use Symfony \Component \HttpKernel \Exception \HttpException ;
13
+ use Symfony \Component \HttpKernel \Exception \MethodNotAllowedHttpException ;
12
14
use Whoops \Handler \PrettyPageHandler ;
13
15
use Whoops \Run ;
14
16
@@ -75,7 +77,7 @@ protected function renderExceptionWithWhoops(Exception $e)
75
77
* the array with status and render. If there is any other e.g. fatal or anything
76
78
*
77
79
* @param $request
78
- * @param Exception $e
80
+ * @param Exception $e
79
81
*
80
82
* @return Response|\Symfony\Component\HttpFoundation\Response
81
83
*/
@@ -86,10 +88,19 @@ public function handle($request, Exception $e)
86
88
if ($ e instanceOf BaseException) {
87
89
$ data = $ e ->toArray ();
88
90
} else if ($ e instanceof HttpException) {
91
+
92
+ if ($ e instanceof NotFoundException) {
93
+ $ detail = 'Resource not found ' ;
94
+ } else if ($ e instanceof MethodNotAllowedHttpException) {
95
+ $ detail = 'Method not allowed ' ;
96
+ } else {
97
+ $ detail = $ this ->generateHttpExceptionMessage ($ e );
98
+ }
99
+
89
100
$ data = [
90
101
'status ' => $ e ->getStatusCode (),
91
- 'title ' => str_ireplace ( ' HttpException ' , '' , get_class ( $ e )) ,
92
- 'detail ' => $ e -> getMessage () ,
102
+ 'title ' => $ detail ,
103
+ 'detail ' => $ detail ,
93
104
];
94
105
} else if ($ e instanceof ValidationException) {
95
106
parent ::render ($ request , $ e );
@@ -126,4 +137,20 @@ protected function getExceptionDefaults(Exception $e)
126
137
'type ' => 'https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html ' ,
127
138
];
128
139
}
140
+
141
+ /**
142
+ * @param \Symfony\Component\HttpKernel\Exception\HttpException $e
143
+ *
144
+ * @return string
145
+ */
146
+ private function generateHttpExceptionMessage (HttpException $ e )
147
+ {
148
+ $ class = get_class ($ e );
149
+ $ parts = explode ('\\' , $ class );
150
+
151
+ $ title = $ parts [count ($ parts ) - 1 ] ?? $ class ;
152
+ $ title = str_replace ('HttpException ' , '' , $ title );
153
+
154
+ return $ title ;
155
+ }
129
156
}
0 commit comments