This commit is contained in:
James Cole
2023-01-11 08:13:52 +01:00
parent 732a87f051
commit 3be4c07848

View File

@@ -35,6 +35,7 @@ use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException as LaravelValidationException;
use Laravel\Passport\Exceptions\OAuthServerException as LaravelOAuthException;
use League\OAuth2\Server\Exception\OAuthServerException;
@@ -79,39 +80,54 @@ class Handler extends ExceptionHandler
*/
public function render($request, Throwable $e)
{
if ($e instanceof LaravelValidationException && $request->expectsJson()) {
$expectsJson = $request->expectsJson();
// if the user requests anything /api/, assume the user wants to see JSON.
if (str_starts_with($request->getRequestUri(), '/api/')) {
Log::debug('API endpoint, always assume user wants JSON.');
$expectsJson = true;
}
Log::debug('Now in Handler::render()');
if ($e instanceof LaravelValidationException && $expectsJson) {
// ignore it: controller will handle it.
Log::debug('Return to parent to handle LaravelValidationException');
return parent::render($request, $e);
}
if ($e instanceof NotFoundHttpException && $request->expectsJson()) {
if ($e instanceof NotFoundHttpException && $expectsJson) {
// JSON error:
Log::debug('Return JSON not found error.');
return response()->json(['message' => 'Resource not found', 'exception' => 'NotFoundHttpException'], 404);
}
if ($e instanceof AuthenticationException && $request->expectsJson()) {
if ($e instanceof AuthenticationException && $expectsJson) {
// somehow Laravel handler does not catch this:
Log::debug('Return JSON unauthenticated error.');
return response()->json(['message' => 'Unauthenticated', 'exception' => 'AuthenticationException'], 401);
}
if ($e instanceof OAuthServerException && $request->expectsJson()) {
if ($e instanceof OAuthServerException && $expectsJson) {
Log::debug('Return JSON OAuthServerException.');
// somehow Laravel handler does not catch this:
return response()->json(['message' => $e->getMessage(), 'exception' => 'OAuthServerException'], 401);
}
if ($e instanceof BadRequestHttpException) {
Log::debug('Return JSON BadRequestHttpException.');
return response()->json(['message' => $e->getMessage(), 'exception' => 'BadRequestHttpException'], 400);
}
if ($e instanceof BadHttpHeaderException) {
// is always API exception.
Log::debug('Return JSON BadHttpHeaderException.');
return response()->json(['message' => $e->getMessage(), 'exception' => 'BadHttpHeaderException'], $e->statusCode);
}
if ($request->expectsJson()) {
if ($expectsJson) {
$errorCode = 500;
$errorCode = $e instanceof MethodNotAllowedHttpException ? 405 : $errorCode;
$isDebug = config('app.debug', false);
if ($isDebug) {
Log::debug(sprintf('Return JSON %s with debug.', get_class($e)));
return response()->json(
[
'message' => $e->getMessage(),
@@ -123,7 +139,7 @@ class Handler extends ExceptionHandler
$errorCode
);
}
Log::debug(sprintf('Return JSON %s.', get_class($e)));
return response()->json(
['message' => sprintf('Internal Firefly III Exception: %s', $e->getMessage()), 'exception' => get_class($e)],
$errorCode
@@ -131,23 +147,28 @@ class Handler extends ExceptionHandler
}
if ($e instanceof NotFoundHttpException) {
Log::debug('Refer to GracefulNotFoundHandler');
$handler = app(GracefulNotFoundHandler::class);
return $handler->render($request, $e);
}
if ($e instanceof FireflyException || $e instanceof ErrorException || $e instanceof OAuthServerException) {
$isDebug = config('app.debug');
return response()->view('errors.FireflyException', ['exception' => $e, 'debug' => $isDebug], 500);
}
// special view for database errors with extra instructions
if ($e instanceof QueryException) {
Log::debug('Return Firefly III database exception view.');
$isDebug = config('app.debug');
return response()->view('errors.DatabaseException', ['exception' => $e, 'debug' => $isDebug], 500);
}
//var_dump($e);exit;
if ($e instanceof FireflyException || $e instanceof ErrorException || $e instanceof OAuthServerException) {
Log::debug('Return Firefly III error view.');
$isDebug = config('app.debug');
return response()->view('errors.FireflyException', ['exception' => $e, 'debug' => $isDebug], 500);
}
Log::debug('Error has no Firefly III treatment, parent will handle.');
return parent::render($request, $e);
}