You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* If the user submitting the request is the restaurant's owner,
170
170
* we don't allow the review creation.
171
-
*/
171
+
*/
172
172
if (user.id===restaurant.owner.id) {
173
173
// highlight-start
174
174
/**
175
175
* Throws a custom policy error
176
176
* instead of just returning false
177
177
* (which would result into a generic Policy Error).
178
-
*/
179
-
thrownewPolicyError('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
+
consterror=newApplicationError(
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;
182
188
// highlight-end
183
189
}
184
190
@@ -200,7 +206,7 @@ When a policy refuses access to a route and a default error is thrown, the follo
200
206
"data":null,
201
207
"error": {
202
208
"status":403,
203
-
"name":"PolicyError",
209
+
"name":"ForbiddenError",
204
210
"message":"Policy Failed",
205
211
"details": {}
206
212
}
@@ -213,12 +219,14 @@ When a policy refuses access to a route and a default error is thrown, the follo
213
219
214
220
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:
215
221
222
+
Note that because `ForbiddenError` (403) is always replaced with a generic message, we used an `ApplicationError` (400) to send the custom message.
223
+
216
224
```jsx
217
225
{
218
226
"data":null,
219
227
"error": {
220
-
"status":403,
221
-
"name":"PolicyError",
228
+
"status":400,
229
+
"name":"OwnerReviewError",
222
230
"message":"The owner of the restaurant cannot submit reviews",
Note: `ForbiddenError` message contents will not be displayed to the Content API and will be returned to the user as an empty `UnauthorizedError`
409
+
408
410
```js
409
411
throw new ForbiddenError('Ah ah ah, you didn\'t say the magic word');
410
412
```
@@ -419,6 +421,8 @@ The `UnauthorizedError` class is a specific error class used when a user doesn't
419
421
| --- | --- | --- | --- |
420
422
|`message`|`string`| The error message |`Unauthorized`|
421
423
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
+
422
426
```js
423
427
thrownewUnauthorizedError('You shall not pass!');
424
428
```
@@ -466,6 +470,8 @@ The `PolicyError` class is a specific error designed to be used with [route poli
466
470
thrownewPolicyError('Something went wrong', { policy:'my-policy' });
467
471
```
468
472
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.
0 commit comments