@@ -136,7 +136,7 @@ use KodePandai\ApiResponse\ExceptionHandler as ApiExceptionHandler;
136
136
public function register()
137
137
{
138
138
$this->renderable(function (Throwable $e, $request) {
139
- if ($request->wantsJson() || str_contains( $request->path(), ' api')) {
139
+ if ($request->wantsJson() || $request->is('* api* ')) {
140
140
return ApiExceptionHandler::renderAsApiResponse($e);
141
141
}
142
142
});
@@ -145,7 +145,7 @@ public function register()
145
145
// old laravel (<= 7)
146
146
public function render($request, Throwable $exception)
147
147
{
148
- if ($request->wantsJson() || str_contains( $request->path(), ' api')) {
148
+ if ($request->wantsJson() || $request->is('* api* ')) {
149
149
return ApiExceptionHandler::renderAsApiResponse($exception);
150
150
}
151
151
@@ -155,8 +155,9 @@ public function render($request, Throwable $exception)
155
155
156
156
### Handling Exception Manually
157
157
158
- If you want to handle all exceptions manually, please not that you must convert ` ApiResponse ` to ` JsonResponse ` by call
159
- ` toResponse ` method explicitly. See this example:
158
+ If you want to handle exception manually,
159
+ please note that you must convert ` ApiResponse ` to ` JsonResponse `
160
+ by calling ` toResponse ` method explicitly. See this example:
160
161
161
162
``` php
162
163
// file: Exception/Handler.php
@@ -167,7 +168,7 @@ public function register()
167
168
$this->renderable(function (AuthenticationException $e, Request $request) {
168
169
return ApiResponse::error()
169
170
->message('Unauthorized')
170
- ->statusCode(401 )
171
+ ->statusCode(Response::HTTP_UNAUTHORIZED )
171
172
->toResponse($request); // this part is required
172
173
});
173
174
}
@@ -178,7 +179,7 @@ public function render($request, Throwable $exception)
178
179
if($exception instanceof AuthenticationException){
179
180
return ApiResponse::error()
180
181
->message('Unauthorized')
181
- ->statusCode(401 )
182
+ ->statusCode(Response::HTTP_UNAUTHORIZED )
182
183
->toResponse($request); // this part is required
183
184
}
184
185
0 commit comments