Although this question was asked in the last century, I like to leave an answer for Laravel 9.7.0.
This answer shows how to globally set how unhandled exceptions should be rendered in http-responses.
I changed the code a bit to return the exception-info without a stack-trace:
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
$this->renderable(function (Throwable $e, $request) {
$env = config("app.env");
$msg = $e->getMessage() . " in file '" . $e->getFile() . "' on line '" . $e->getLine() . "'";
if ($request->is('api/*')) {
if (strcmp($env, "debug") == 0) {
return response()->make($msg.$e->getTraceAsString(), 500);
} else {
return response()->json([
'message' => $msg
], 500);
}
}
});
}
So if I set APP_ENV=debug
in the .env-file the stacktrace will be added to the repsonse text-message. It's text because I did not test how the stacktrace would mess with the consumers json interpreter. If APP_ENV != debug, the response is a json object with a message-value containing the message, file and linenumber.