Skip to content
This repository was archived by the owner on Sep 16, 2025. It is now read-only.

Commit 5752ccf

Browse files
fix(controller): errors should not result in internal error
1 parent f7ba4cc commit 5752ccf

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

server/controllers/slug-controller.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const _ = require('lodash');
4-
const { NotFoundError } = require('@strapi/utils').errors;
54
const { getPluginService } = require('../utils/getPluginService');
65
const { isValidFindSlugParams } = require('../utils/isValidFindSlugParams');
76
const { sanitizeOutput } = require('../utils/sanitizeOutput');
@@ -14,15 +13,23 @@ module.exports = ({ strapi }) => ({
1413
const { modelName, slug } = ctx.request.params;
1514
const { auth } = ctx.state;
1615

17-
isValidFindSlugParams({
18-
modelName,
19-
slug,
20-
modelsByName,
21-
});
16+
try {
17+
isValidFindSlugParams({
18+
modelName,
19+
slug,
20+
modelsByName,
21+
});
22+
} catch (error) {
23+
return ctx.badRequest(error.message);
24+
}
2225

2326
const { uid, field, contentType } = modelsByName[modelName];
2427

25-
await hasRequiredModelScopes(strapi, uid, auth);
28+
try {
29+
await hasRequiredModelScopes(strapi, uid, auth);
30+
} catch (error) {
31+
return ctx.forbidden();
32+
}
2633

2734
// add slug filter to any already existing query restrictions
2835
let query = ctx.query || {};
@@ -42,7 +49,7 @@ module.exports = ({ strapi }) => ({
4249
const sanitizedEntity = await sanitizeOutput(data, contentType, auth);
4350
ctx.body = transform.response({ data: sanitizedEntity, schema: contentType });
4451
} else {
45-
throw new NotFoundError();
52+
ctx.notFound();
4653
}
4754
},
4855
});

server/utils/hasRequiredModelScopes.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
const { ForbiddenError } = require('@strapi/utils').errors;
2-
3-
const hasRequiredModelScopes = async (strapi, uid, auth) => {
4-
try {
5-
await strapi.auth.verify(auth, { scope: `${uid}.find` });
6-
} catch (e) {
7-
throw new ForbiddenError();
8-
}
9-
};
1+
const hasRequiredModelScopes = (strapi, uid, auth) =>
2+
strapi.auth.verify(auth, { scope: `${uid}.find` });
103

114
module.exports = {
125
hasRequiredModelScopes,

0 commit comments

Comments
 (0)