Skip to content

Commit fcf2a86

Browse files
committed
feat: Update error handling for blocked paths and parameter limits to return 400 status with descriptive messages
1 parent 80267dc commit fcf2a86

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/controllers/cdnController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export const proxyReportsFile = async (req, res, filePath) => {
3434

3535
// Block access to crawls and results paths
3636
if (filePath.startsWith('crawls/') || filePath.startsWith('results/')) {
37-
res.statusCode = 403;
38-
res.end(JSON.stringify({ error: 'Access denied' }));
37+
res.statusCode = 400;
38+
res.end(JSON.stringify({ error: 'Not supported. Response size too large.' }));
3939
return;
4040
}
4141

src/tests/routes.test.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ describe('API Routes', () => {
142142
expect(Array.isArray(res.body)).toBe(true);
143143
});
144144

145+
it('should return 400 when category filter exceeds limit', async () => {
146+
const tooManyCategories = Array.from({ length: 31 }, (_, index) => `cat${index}`).join(',');
147+
const res = await request(app).get(`/v1/technologies?category=${encodeURIComponent(tooManyCategories)}`);
148+
149+
expect(res.statusCode).toEqual(400);
150+
expect(res.body).toHaveProperty('errors');
151+
expect(res.body.errors[0]).toHaveProperty('error');
152+
expect(res.body.errors[0].error).toContain('Too many values specified for category');
153+
});
154+
145155
it('should handle CORS preflight requests', async () => {
146156
const res = await request(app)
147157
.options('/v1/technologies')
@@ -471,25 +481,25 @@ describe('API Routes', () => {
471481
});
472482

473483
describe('Blocked paths (crawls and results)', () => {
474-
it('should block access to crawls paths with 403', async () => {
484+
it('should block access to crawls paths with 400', async () => {
475485
const res = await request(app)
476486
.get('/v1/static/crawls/chrome-Jan_1_2026/260113_Dx1LM_CCNR1.har.gz')
477-
.expect(403);
478-
expect(res.body).toHaveProperty('error', 'Access denied');
487+
.expect(400);
488+
expect(res.body).toHaveProperty('error', 'Not supported. Response size too large.');
479489
});
480490

481-
it('should block access to results paths with 403', async () => {
491+
it('should block access to results paths with 400', async () => {
482492
const res = await request(app)
483493
.get('/v1/static/results/250114_Dx0_1.zip')
484-
.expect(403);
485-
expect(res.body).toHaveProperty('error', 'Access denied');
494+
.expect(400);
495+
expect(res.body).toHaveProperty('error', 'Not supported. Response size too large.');
486496
});
487497

488498
it('should block crawls paths at any depth', async () => {
489499
const res = await request(app)
490500
.get('/v1/static/crawls/some/nested/path/file.tar.gz')
491-
.expect(403);
492-
expect(res.body).toHaveProperty('error', 'Access denied');
501+
.expect(400);
502+
expect(res.body).toHaveProperty('error', 'Not supported. Response size too large.');
493503
});
494504

495505
it('should allow other paths like reports', async () => {

src/utils/controllerHelpers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ const validateArrayParameter = (value, fieldName = 'parameter') => {
7272
const valueArray = convertToArray(value);
7373

7474
if (valueArray.length > FIRESTORE_IN_LIMIT) {
75-
throw new Error(`Too many values specified for ${fieldName}. Maximum ${FIRESTORE_IN_LIMIT} allowed.`);
75+
const error = new Error(`Too many values specified for ${fieldName}. Maximum ${FIRESTORE_IN_LIMIT} allowed.`);
76+
error.statusCode = 400;
77+
throw error;
7678
}
7779

7880
return valueArray;

0 commit comments

Comments
 (0)