From 56261eadc53fc17d4a02ccd4de3bfd136ddf7ddb Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Thu, 10 Oct 2024 22:48:10 +0100 Subject: [PATCH] Fix: Add call to getPrevious in exception handler to get actull thrown error --- app/Services/ExceptionHandler.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/app/Services/ExceptionHandler.php b/app/Services/ExceptionHandler.php index 5a2ffee..64fbe32 100644 --- a/app/Services/ExceptionHandler.php +++ b/app/Services/ExceptionHandler.php @@ -29,34 +29,37 @@ public static function render($request, Throwable $e) static::$request = $request; if ($request->isXmlHttpRequest() || request()->is('api/*')) { - $line = method_exists($e, 'getFile') ? ' in '.$e->getFile() : ''; - $line .= method_exists($e, 'getLine') ? ' on line '.$e->getLine() : ''; - $msg = method_exists($e, 'getMessage') ? $e->getMessage().$line : 'An error occured'.$line; + $line = method_exists($e, 'getFile') ? ' in ' . $e->getFile() : ''; + $line .= method_exists($e, 'getLine') ? ' on line ' . $e->getLine() : ''; + $msg = method_exists($e, 'getMessage') ? $e->getMessage() . $line : 'An error occured' . $line; $plainMessage = method_exists($e, 'getMessage') ? $e->getMessage() : null; if ((bool) collect($e?->getTrace())->firstWhere('function', 'abort')) { static::$message = $e->getMessage(); } - $prefix = $e instanceof ModelNotFoundException ? str($e->getModel())->afterLast('\\')->lower() : ''; + $exception = $e->getPrevious() ?: $e; + $prefix = $exception instanceof ModelNotFoundException + ? str($exception->getModel())->afterLast('\\')->lower() + : ''; return match (true) { $e instanceof NotFoundHttpException || - $e instanceof ModelNotFoundException => static::renderException( + $e instanceof ModelNotFoundException => static::renderException( str(str($msg)->contains('The route') ? str($msg)->before('.')->append('.') : HttpStatus::message(HttpStatus::NOT_FOUND))->replace('resource', $prefix ?: 'resource'), HttpStatus::NOT_FOUND ), $e instanceof \Spatie\Permission\Exceptions\UnauthorizedException || - $e instanceof AuthorizationException || - $e instanceof AccessDeniedHttpException || - $e->getCode() === HttpStatus::FORBIDDEN => static::renderException( + $e instanceof AuthorizationException || + $e instanceof AccessDeniedHttpException || + $e->getCode() === HttpStatus::FORBIDDEN => static::renderException( $plainMessage ? $plainMessage : HttpStatus::message(HttpStatus::FORBIDDEN), HttpStatus::FORBIDDEN ), $e instanceof AuthenticationException || - $e instanceof UnauthorizedHttpException => static::renderException( + $e instanceof UnauthorizedHttpException => static::renderException( HttpStatus::message(HttpStatus::UNAUTHORIZED), HttpStatus::UNAUTHORIZED ),