Skip to content

Commit

Permalink
Handle AuthorizationException
Browse files Browse the repository at this point in the history
  • Loading branch information
ttrig committed Feb 2, 2024
1 parent 30b8c1e commit 8bdcfd3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Support extending schema using partial GraphQL schema files.

### Added
- Handle `Illuminate\Auth\Access\AuthorizationException` exceptions.

## [10.0.0] - 2023-03-02

### Changed
Expand Down
11 changes: 11 additions & 0 deletions src/Concerns/HandlesGraphqlRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use GraphQL\Type\Schema;
use GraphQL\Utils\BuildSchema;
use GraphQL\Utils\SchemaExtender;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Database\Eloquent\MissingAttributeException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Expand Down Expand Up @@ -109,6 +110,16 @@ public function errorFormatter(GraphqlError $graphqlError)
$throwable instanceof Exception ? $throwable : $graphqlError
);

if ($throwable instanceof AuthorizationException) {
return array_merge($formattedError, [
'message' => $throwable->getMessage(),
'extensions' => [
'category' => 'client',
'code' => $throwable->status() ?: 403,
],
]);
}

if (
$throwable instanceof HttpException &&
$throwable->getStatusCode() >= 400 &&
Expand Down
12 changes: 12 additions & 0 deletions tests/HandlesGraphqlRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ public function test_error_with_trace()
$this->assertSame('internal', Arr::get($data, 'errors.0.extensions.category'));
}

public function test_authorization_error_is_formatted()
{
$controller = $this->app->make(GraphqlController::class);
$data = $controller(Request::create('/', 'POST', [
'query' => '{ throwAuthorizationException }',
]));

$this->assertSame('This action is unauthorized.', data_get($data, 'errors.0.message'));
$this->assertSame('client', data_get($data, 'errors.0.extensions.category'));
$this->assertSame(403, data_get($data, 'errors.0.extensions.code'));
}

public function test_http_client_error_is_formatted()
{
$controller = $this->app->make(GraphqlController::class);
Expand Down
13 changes: 13 additions & 0 deletions tests/stubs/Queries/ThrowAuthorizationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Butler\Graphql\Tests\Queries;

use Illuminate\Auth\Access\AuthorizationException;

class ThrowAuthorizationException
{
public function __invoke($root, $args, $context)
{
throw new AuthorizationException();
}
}
1 change: 1 addition & 0 deletions tests/stubs/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Query {
testFieldResolver: [NullingType!]!
dataLoader: [Thing]
dataLoaderWithCollections: [Thing!]!
throwAuthorizationException: String!
throwError: String!
throwException: String!
throwHttpException(code: Int = 400): String!
Expand Down

0 comments on commit 8bdcfd3

Please sign in to comment.