hapi icon indicating copy to clipboard operation
hapi copied to clipboard

Application errors are not logging, only generating Boom "Internal Server Error" logs

Open geuis opened this issue 7 months ago • 2 comments

Runtime

node

Runtime version

21.1

Module version

huh?

Used with

hapi 21.3.2

Any other relevant information

No response

How can we help?

Ran into an issue where errors generated by application code are having their errors suppressed run in debug mode locally.

I have a route that is generating an error. When running locally, I'm just getting Boom internal server errors and not getting the actual error log messages.

I have the route wrapped in a try/catch and I can console.error the full logs in the catch, but I feel like there should be a global routes setting somewhere like debug that will just dump all errors like this to the console without needing special handling each time.

Note that failAction below does not get triggered either.

I've read up on debug https://hapi.dev/api/?v=21.3.2#-serveroptionsdebug though maybe I'm missing something?

My current server config:

  const server = Hapi.server({
    port: 2000,
    host: '0.0.0.0',
    routes: {
      validate: {
        failAction: async (req, h, err) => {
          console.error(err);
          throw err;
        }
      }
    },
    debug: {
      log: ['*'],
      request: ['*']
    },
  });

geuis avatar Nov 18 '23 22:11 geuis

I'm adding some follow up to this since its been a few weeks without any response.

I've been through the documentation on logging:

  • https://hapi.dev/api/?v=21.3.2#-serveroptionsdebug
  • https://hapi.dev/tutorials/logging/?lang=en_US

Here's a sample route for some Stripe webhooks I'm working on:

  server.route({
    path: '/api/v1/webhooks',
    method: 'post',
    options: {
      auth: false,
      payload: {
        parse: false
      }
    },
    handler: async (req, h) => {
      let sig;
      let stripeEvent;

      try {
        sig = req.headers['stripe-signature'];
        stripeEvent = stripeClient.webhooks.constructEvent(req.payload.toString(), sig, process.env.STRIPE_WEBHOOK_ENDPOINT_SECRET);
      } catch (err) {
        // this logs to the console.
        console.log('webhook signature verification failed:', err.message);
        return h.response().code(400);
      }

      try {
        const { type: eventType } = stripeEvent;

        switch (eventType) {
          // this contains some code that intentionally causes an error when I run "stripe trigger customer.subscription.deleted"

            break;
          }

          default:
            console.log(`Unhandled webhook: ${eventType}`);
            break;
        }

        return h.response().code(200);
      } catch (err) {
        // not seen
        process.stdout.write(err);

        // not seen
        console.error(`Webhook eventType: ${eventType}`, err);
        
        // not seen
        server.log('error', `Webhook eventType: ${eventType}`);

        // not seen 
        req.log('error', `Webhook eventType: ${eventType}`);

        throw err;
      }

All I get in the terminal is

Debug: handler, error 
    {"code":"ERR_UNKNOWN_ENCODING","isBoom":true,"isServer":true,"data":null,"output":{"statusCode":500,"payload":{"statusCode":500,"error":"Internal Server Error","message":"An internal server error occurred"},"headers":{}},"isDeveloperError":true}
2023-12-09 23:31:40  <--  [500] POST http://localhost:2000/api/v1/webhooks [evt_1OLhUFCKVl5kfDhd9LsStTaA]

I'm probably missing something, but I've been through the docs and I think I have this configured correctly. However, the callstack for the errors are being hidden.

geuis avatar Dec 10 '23 07:12 geuis

You are probably not seeing the stack trace since you are using a not yet supported node runtime (v21.x), where they can go missing due to a change described in https://github.com/hapijs/boom/issues/302.

For the current version of hapi I would advise you to stay on node v18 for the time being.

kanongil avatar Dec 11 '23 09:12 kanongil