Skip to content

Commit

Permalink
fix: fix incorrect examples (#1959)
Browse files Browse the repository at this point in the history
  • Loading branch information
innerdvations authored Dec 20, 2023
1 parent 0e9f3fa commit 655d8c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,23 @@ module.exports = async (policyContext, config, { strapi }) => {
/**
* If the user submitting the request is the restaurant's owner,
* we don't allow the review creation.
*/
*/
if (user.id === restaurant.owner.id) {
// highlight-start
/**
* Throws a custom policy error
* instead of just returning false
* (which would result into a generic Policy Error).
*/
throw new PolicyError('The owner of the restaurant cannot submit reviews', {
errCode: 'RESTAURANT_OWNER_REVIEW', // can be useful for identifying different errors on the front end
});
*/
const error = new ApplicationError(
"The owner of the restaurant cannot submit reviews",
{
policy: "is-owner-review",
errCode: "RESTAURANT_OWNER_REVIEW", // can be useful for identifying different errors on the front end
}
);
error.name = "OwnerReviewError";
throw error;
// highlight-end
}

Expand All @@ -200,7 +206,7 @@ When a policy refuses access to a route and a default error is thrown, the follo
"data": null,
"error": {
"status": 403,
"name": "PolicyError",
"name": "ForbiddenError",
"message": "Policy Failed",
"details": {}
}
Expand All @@ -213,12 +219,14 @@ When a policy refuses access to a route and a default error is thrown, the follo

When a policy refuses access to a route and the custom policy throws the custom error defined in the code example above, the following response will be sent when trying to query the content-type through the REST API:

Note that because `ForbiddenError` (403) is always replaced with a generic message, we used an `ApplicationError` (400) to send the custom message.

```jsx
{
"data": null,
"error": {
"status": 403,
"name": "PolicyError",
"status": 400,
"name": "OwnerReviewError",
"message": "The owner of the restaurant cannot submit reviews",
"details": {
"policy": "is-owner-review",
Expand Down
6 changes: 6 additions & 0 deletions docusaurus/docs/dev-docs/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ The `ForbiddenError` class is a specific error class used when a user either doe
| --- | --- | --- | --- |
| `message` | `string` | The error message | `Forbidden access` |
Note: `ForbiddenError` message contents will not be displayed to the Content API and will be returned to the user as an empty `UnauthorizedError`
```js
throw new ForbiddenError('Ah ah ah, you didn\'t say the magic word');
```
Expand All @@ -419,6 +421,8 @@ The `UnauthorizedError` class is a specific error class used when a user doesn't
| --- | --- | --- | --- |
| `message` | `string` | The error message | `Unauthorized` |

Note: `UnauthorizedError` message contents will not be displayed to the Content API and will be returned to the user as an empty `UnauthorizedError`

```js
throw new UnauthorizedError('You shall not pass!');
```
Expand Down Expand Up @@ -466,6 +470,8 @@ The `PolicyError` class is a specific error designed to be used with [route poli
throw new PolicyError('Something went wrong', { policy: 'my-policy' });
```

Note: Because `PolicyError` extends `ForbiddenError`, it will not be displayed to the Content API and will be returned to the user as an empty `ForbiddenError` and you will need to use a different error type in your policy if you want it to be visible in the Content API.

</TabItem>

</Tabs>

1 comment on commit 655d8c9

@vercel
Copy link

@vercel vercel bot commented on 655d8c9 Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

documentation – ./

docs-vercel-v4.strapi.io
documentation-strapijs.vercel.app
documentation-git-main-strapijs.vercel.app

Please sign in to comment.