Skip to content

Commit e7112ea

Browse files
authored
Merge pull request #236 from immobiliare/404_fix_2
fix: skip getLabel on 404
2 parents 9ef1e9b + 37b6e2e commit e7112ea

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

examples/nesting.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ app.register(plugin, {
3434
host: `udp://127.0.0.1:${udpPort}`,
3535
namespace: 'nesting_upd_test',
3636
},
37-
routes: false
37+
routes: false,
3838
});
3939

4040
app.register(

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ module.exports = fp(
225225
fastify.decorateRequest(kMetricsLabel, '');
226226
fastify.decorateReply(kMetricsLabel, '');
227227
fastify.addHook('onRequest', function (request, reply, next) {
228-
const label = getLabel.call(this, request, reply);
228+
// For some reason, this request.is404 is false on 404s only in tests.
229+
/* istanbul ignore next */
230+
const label = request.is404
231+
? ''
232+
: getLabel.call(this, request, reply);
229233
request[kMetricsLabel] = label;
230234
reply[kMetricsLabel] = label;
231235
next();

tests/dynamic-mode.test.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -410,25 +410,25 @@ tap.test('custom getLabel and custom prefix', async (t) => {
410410
app,
411411
t
412412
);
413+
});
413414

414-
tap.test('404 test', async (t) => {
415-
const server = await setup(
416-
{
417-
client: {
418-
namespace: '404_dynamic_mode_errors',
419-
},
420-
health: false,
415+
tap.test('404 test', async (t) => {
416+
const server = await setup(
417+
{
418+
client: {
419+
namespace: '404_dynamic_mode_errors',
421420
},
422-
undefined,
423-
t
424-
);
425-
t.teardown(async () => {
426-
return server.close();
427-
});
428-
const response = await server.inject({
429-
method: 'GET',
430-
url: '/not-existing',
431-
});
432-
t.equal(404, response.statusCode);
421+
health: false,
422+
},
423+
undefined,
424+
t
425+
);
426+
t.teardown(async () => {
427+
return server.close();
428+
});
429+
const response = await server.inject({
430+
method: 'GET',
431+
url: '/not-existingasdfasdfasdfasdfasdfasdfasd',
433432
});
433+
t.equal(404, response.statusCode);
434434
});

0 commit comments

Comments
 (0)