Skip to content

Commit

Permalink
Fix: Add call to getPrevious in exception handler to get actull throw…
Browse files Browse the repository at this point in the history
…n error
  • Loading branch information
3m1n3nc3 committed Oct 10, 2024
1 parent 676a343 commit 56261ea
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions app/Services/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand Down

0 comments on commit 56261ea

Please sign in to comment.