Skip to content

Commit 589fa47

Browse files
committed
Add Forbidden error details
1 parent 8584d1d commit 589fa47

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

src/Endpoint/Create.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public function handle(Context $context, ResourceType $resourceType): ResponseIn
3333
$schema = $resourceType->getSchema();
3434

3535
if (! evaluate($schema->isCreatable(), [$context])) {
36-
throw new ForbiddenException();
36+
throw new ForbiddenException(sprintf(
37+
'Cannot create resource type %s',
38+
$resourceType->getType()
39+
));
3740
}
3841

3942
$model = $this->newModel($resourceType, $context);

src/Endpoint/Delete.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public function handle(Context $context, ResourceType $resourceType, $model): Re
3030
$schema = $resourceType->getSchema();
3131

3232
if (! evaluate($schema->isDeletable(), [$model, $context])) {
33-
throw new ForbiddenException();
33+
throw new ForbiddenException(sprintf(
34+
'Cannot delete resource %s:%s',
35+
$resourceType->getType(),
36+
$resourceType->getAdapter()->getId($model)
37+
));
3438
}
3539

3640
run_callbacks($schema->getListeners('deleting'), [&$model, $context]);

src/Endpoint/Index.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public function handle(Context $context, ResourceType $resourceType): ResponseIn
4040
$schema = $resourceType->getSchema();
4141

4242
if (! evaluate($schema->isListable(), [$context])) {
43-
throw new ForbiddenException();
43+
throw new ForbiddenException(sprintf(
44+
'Cannot list resource type %s',
45+
$resourceType->getType()
46+
));
4447
}
4548

4649
$query = $adapter->query();

src/Endpoint/Update.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function handle(Context $context, ResourceType $resourceType, $model): Re
3131
$schema = $resourceType->getSchema();
3232

3333
if (! evaluate($schema->isUpdatable(), [$model, $context])) {
34-
throw new ForbiddenException();
34+
throw new ForbiddenException(sprintf(
35+
'Cannot update resource %s:%s',
36+
$resourceType->getType(),
37+
$resourceType->getAdapter()->getId($model)
38+
));
3539
}
3640

3741
$data = $this->parseData($resourceType, $context->getRequest()->getParsedBody(), $model);

src/Exception/ForbiddenException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public function getJsonApiErrors(): array
2222
return [
2323
new Error(
2424
new Error\Title('Forbidden'),
25-
new Error\Status($this->getJsonApiStatus())
25+
new Error\Status($this->getJsonApiStatus()),
26+
...($this->message ? [new Error\Detail($this->message)] : [])
2627
)
2728
];
2829
}

0 commit comments

Comments
 (0)