Skip to content

Hapi: route.method.toUpperCase is not a function for routes registered with an array of methods #25481

Open

Description

@joshkel

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node - hapi

SDK Version

11.5.0; also present in 11.8.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

No response

Steps to Reproduce

instrument.mjs:

import * as Sentry from '@sentry/node';
Sentry.init({
  dsn: 'https://public@example.invalid/1',
  tracesSampleRate: 1,
  traceLifecycle: 'static',
});

server.mjs:

import Hapi from '@hapi/hapi';
const server = Hapi.server({ port: 0 });
server.route({ method: ['GET', 'POST'], path: '/multi', handler: () => 'ok' });
server.route({ method: 'GET', path: '/single', handler: () => 'ok' });
await server.start();
for (const path of ['/single', '/multi']) {
  const res = await fetch(server.info.uri + path);
  console.log(path, res.status);
}
await server.stop();

Run node --import ./instrument.mjs server.mjs

Expected Result

Both requests return 200.

Actual Result

/single returns 200; /multi returns 500:

TypeError: route.method.toUpperCase is not a function
    at getRouteMetadata (@sentry/server-utils/src/integrations/hapi/hapi-utils.ts:132:73)
    at <anonymous> (@sentry/server-utils/src/integrations/hapi/hapi-utils.ts:202:24)
    at exports.Manager.execute (@hapi/hapi/lib/toolkit.js:57:29)
    at internals.handler (@hapi/hapi/lib/handler.js:46:48)
    ...

With the default traceLifecycle ('stream'), both return 200.

Additional Context

Hapi accepts method as a string or an array of strings. wrapRouteHandler passes the route config given to server.route() to getRouteMetadata. For non-streamed spans, that builds the span name as `${route.method.toUpperCase()} ${route.path}`, which assumes a string. The streamed-span branch only uses route.path, which is why the default lifecycle is unaffected. Either way, the array also ends up in the http.request.method attribute.

A possible fix: inside the wrapped handler, read the method from the request (request.method, which Hapi has already resolved to one lowercase method) rather than from the route config.

Priority

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions