I have a laravel 12 app that works with Oauth via Passport. All is fine but when i make bogus Auth requests with non valid tokens i get huge stacktraces. I have managed to use withExceptions in app.php but this only works for the OAuth error on the access token (9). The Error on the refresh token (8) still dumps a huge stacktrace. Where am I going wrong? Additionally returning anything but false on (9) also gives me the stacktrace.
return Application::configure(basePath: dirname(__DIR__))
...
->withExceptions(function (Exceptions $exceptions) {
// $exceptions->dontReportDuplicates();
// Handle Laravel Passport authentication errors
$exceptions->report(function (OAuthServerException $e) {
// this works
if ($e->getCode() == 9) {
Log::info('->withExceptions: Error within OAuthServerException (Token probably expired / not found)', []);
// this supresses Passports own Logging and Stacktrace in the log:
// I have not found a way to suppress that AND return a custom json response here
return false;
}
// this does not work
if ($e->getCode() == 8) {
Log::info('->withExceptions: Error within OAuthServerException (Refresh-Token probably expired / not found)', []);
return false;
}
return response()->json([
'message' => 'Authentication error',
'error' => $e->getMessage()
], 401);
});
...
Thanks