Skip to content

Commit 655d8c9

Browse files
fix: fix incorrect examples (#1959)
1 parent 0e9f3fa commit 655d8c9

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

docusaurus/docs/dev-docs/backend-customization/examples/policies.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,23 @@ module.exports = async (policyContext, config, { strapi }) => {
168168
/**
169169
* If the user submitting the request is the restaurant's owner,
170170
* we don't allow the review creation.
171-
*/
171+
*/
172172
if (user.id === restaurant.owner.id) {
173173
// highlight-start
174174
/**
175175
* Throws a custom policy error
176176
* instead of just returning false
177177
* (which would result into a generic Policy Error).
178-
*/
179-
throw new PolicyError('The owner of the restaurant cannot submit reviews', {
180-
errCode: 'RESTAURANT_OWNER_REVIEW', // can be useful for identifying different errors on the front end
181-
});
178+
*/
179+
const error = new ApplicationError(
180+
"The owner of the restaurant cannot submit reviews",
181+
{
182+
policy: "is-owner-review",
183+
errCode: "RESTAURANT_OWNER_REVIEW", // can be useful for identifying different errors on the front end
184+
}
185+
);
186+
error.name = "OwnerReviewError";
187+
throw error;
182188
// highlight-end
183189
}
184190

@@ -200,7 +206,7 @@ When a policy refuses access to a route and a default error is thrown, the follo
200206
"data": null,
201207
"error": {
202208
"status": 403,
203-
"name": "PolicyError",
209+
"name": "ForbiddenError",
204210
"message": "Policy Failed",
205211
"details": {}
206212
}
@@ -213,12 +219,14 @@ When a policy refuses access to a route and a default error is thrown, the follo
213219

214220
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:
215221

222+
Note that because `ForbiddenError` (403) is always replaced with a generic message, we used an `ApplicationError` (400) to send the custom message.
223+
216224
```jsx
217225
{
218226
"data": null,
219227
"error": {
220-
"status": 403,
221-
"name": "PolicyError",
228+
"status": 400,
229+
"name": "OwnerReviewError",
222230
"message": "The owner of the restaurant cannot submit reviews",
223231
"details": {
224232
"policy": "is-owner-review",

docusaurus/docs/dev-docs/error-handling.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ The `ForbiddenError` class is a specific error class used when a user either doe
405405
| --- | --- | --- | --- |
406406
| `message` | `string` | The error message | `Forbidden access` |
407407
408+
Note: `ForbiddenError` message contents will not be displayed to the Content API and will be returned to the user as an empty `UnauthorizedError`
409+
408410
```js
409411
throw new ForbiddenError('Ah ah ah, you didn\'t say the magic word');
410412
```
@@ -419,6 +421,8 @@ The `UnauthorizedError` class is a specific error class used when a user doesn't
419421
| --- | --- | --- | --- |
420422
| `message` | `string` | The error message | `Unauthorized` |
421423

424+
Note: `UnauthorizedError` message contents will not be displayed to the Content API and will be returned to the user as an empty `UnauthorizedError`
425+
422426
```js
423427
throw new UnauthorizedError('You shall not pass!');
424428
```
@@ -466,6 +470,8 @@ The `PolicyError` class is a specific error designed to be used with [route poli
466470
throw new PolicyError('Something went wrong', { policy: 'my-policy' });
467471
```
468472

473+
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.
474+
469475
</TabItem>
470476

471477
</Tabs>

0 commit comments

Comments
 (0)