Skip to content

Commit

Permalink
refactor: improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Apr 16, 2022
1 parent 7ece8ab commit 361309a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testGetAllUsersByNonAdmin(): void
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('message')
->where('message', 'This action is unauthorized.')
->where('message', 'You are not authorized to request this resource.')
->etc()
);
}
Expand Down
12 changes: 10 additions & 2 deletions app/Ship/Exceptions/Handlers/ExceptionsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace App\Ship\Exceptions\Handlers;

use Apiato\Core\Abstracts\Exceptions\Exception as CoreException;
use Apiato\Core\Exceptions\AuthenticationException;
use Apiato\Core\Exceptions\AuthenticationException as CoreAuthenticationException;
use Apiato\Core\Exceptions\Handlers\ExceptionsHandler as CoreExceptionsHandler;
use App\Ship\Exceptions\NotAuthorizedResourceException;
use App\Ship\Exceptions\NotFoundException;
use Illuminate\Auth\AuthenticationException as LaravelAuthenticationException;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
Expand Down Expand Up @@ -57,7 +60,7 @@ public function register(): void
});

$this->renderable(function (AccessDeniedHttpException $e) {
return $this->buildResponse(new AuthenticationException());
return $this->buildResponse(new NotAuthorizedResourceException());
});
}

Expand All @@ -81,4 +84,9 @@ private function buildResponse(CoreException $e): JsonResponse

return response()->json($response, (int)$e->getCode());
}

protected function unauthenticated($request, LaravelAuthenticationException $exception): JsonResponse|Response
{
return $this->buildResponse(new CoreAuthenticationException());
}
}

0 comments on commit 361309a

Please sign in to comment.