Is you .env set to local or production?
Laravel API 500 Exception Issue
Hi, I'm trying to catch exceptions for my API. Everything like 400, 401, 404, etc all work fine but for 500 errors it doesnt.
$exceptions->render(function (Exception $e, Request $request) {
if ($request->expectsJson() || $request->is('api/*')) {
return response()->json([
'error' => 'Internal Server Error',
'message' => $e->getMessage(),
], 500);
}
});
I'm using that, but the API just returns a 500 HTML page.
If I do:
$exceptions->render(function (Exception $e, Request $request) {
return response()->json([
'error' => 'Internal Server Error',
'message' => $e->getMessage(),
], 500);
});
That works BUT any 500 errors on the web end shows JSON.
I've tried both local and production with debug mode off.
Any ideas how to get around this?
Because I want to return the user a consistent error message/response to the user when using the API for all requests?
@Haych Laravel does this already. It returns the correct HTTP status code, and presents errors using a format like:
{
"message": "Something contextual."
}
Why would I want to return an HTML page to them in the API
You’ll get JSON responses if you request you want a JSON response, by including a Accept: application/json header in your requests.
Please or to participate in this conversation.