Skip to content

Commit

Permalink
fix(core): duplicated path if the error is thrown from parameter (#13967
Browse files Browse the repository at this point in the history
)

* fix(core): double namespace if the error is thrown from parameter

* test: fix test case
  • Loading branch information
TonyRL authored Dec 6, 2023
1 parent 88241f5 commit 7da1620
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
10 changes: 7 additions & 3 deletions lib/middleware/onerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ module.exports = async (ctx, next) => {
});
}
} catch (err) {
// Append v2 route path
ctx.request.path = (ctx.mountPath ?? '') + ctx.request.path;
ctx._matchedRoute = ctx._matchedRoute ? (ctx.mountPath ?? '') + ctx._matchedRoute : ctx.request.path;
if (err instanceof Error && !err.stack.split('\n')[1].includes('lib/middleware/parameter.js')) {
// Append v2 route path if a route throws an error
// since koa-mount will remove the mount path from ctx.request.path
// https://github.com/koajs/mount/issues/62
ctx.request.path = (ctx.mountPath ?? '') + ctx.request.path;
ctx._matchedRoute = ctx._matchedRoute ? (ctx.mountPath ?? '') + ctx._matchedRoute : ctx._matchedRoute;
}

let message = err;
if (err.name && (err.name === 'HTTPError' || err.name === 'RequestError')) {
Expand Down
4 changes: 2 additions & 2 deletions test/middleware/onerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ describe('v2 route throws an error', () => {
expect(value).toBe('7');
break;
case 'Hot Routes:':
expect(value).toBe('4 /test/:id<br>1 /test/slow<br>1 /thisDoesNotExist<br>');
expect(value).toBe('4 /test/:id<br>');
break;
case 'Hot Paths:':
expect(value).toBe('2 /test/error<br>2 /test/slow<br>1 /test/httperror<br>1 /thisDoesNotExist<br>1 /<br>');
break;
case 'Hot Error Routes:':
expect(value).toBe('3 /test/:id<br>1 /test/slow<br>1 /thisDoesNotExist<br>');
expect(value).toBe('3 /test/:id<br>');
break;
case 'Hot Error Paths:':
expect(value).toBe('2 /test/error<br>1 /test/httperror<br>1 /test/slow<br>1 /thisDoesNotExist<br>');
Expand Down
12 changes: 7 additions & 5 deletions test/utils/rand-user-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ describe('rand-user-agent', () => {
});

it('should match ua configurated', async () => {
const response1 = await got('https://httpbingo.org/user-agent');
expect(response1.data['user-agent']).toBe(config.ua);
nock('https://rsshub.test')
.get('/test')
.reply(function () {
return [200, { ua: this.req.headers['user-agent'] }];
});

const response2 = await got('https://httpbingo.org/user-agent', {
const resonse = await got('https://rsshub.test/test', {
headers: {
'user-agent': mobileUa,
},
});
expect(response2.data['user-agent']).toBe(mobileUa);
expect(response2.data['user-agent']).not.toBe(config.ua);
expect(resonse.data.ua).toBe(mobileUa);
});
});

0 comments on commit 7da1620

Please sign in to comment.