Skip to content

Strange behaviour with throw in handlers #2616

@qraynaud

Description

@qraynaud

Hi,

I'm working on latest, so v8.6.1.

I never noticed this before so maybe it's a recent bug, but when I throw in a handler, sometimes the error is sent to the wrong request.

Here is the route that fails:

'use strict';

module.exports = {
  method: 'GET',
  path: '/api/user/_me',
  config: {
    handler: function(request, reply) {
      var user = request.session.get('user');
      if (user)
        return reply(user);
      var err = new Error('You are disconnected');
      err.status = 401;
      throw err;
    }
  }
};

This is only a test to showcase the problem, on a normal day, I would just reply the error.

I don't know if it can be related, but I have the following onPreResponse handler:

// Handle errors
server.ext('onPreResponse', function(request, reply) {
  if (!(request.response instanceof Error) && !request.response.isBoom)
    return reply.continue();

  console.error(chalk.grey('[') + chalk.red('err') + chalk.grey('] on ') +
    chalk.yellow(request.path) + (request.url.search ? chalk.blue(request.url.search) : '') + chalk.grey(':'));
  console.error((request.response.stack || request.response).toString().replace(/^/mg, chalk.grey('> ')));

  reply(_.pick(request.response, ['message', 'status']))
    .code(request.response.status || 500)
  ;
});

I make multiple simultaneous requests using my browser. Most of the time it works but sometimes another request gets the error and I never get to have an answer on the throwing route.

Here is my network stack when it works:
api_success

And here is the result once every 5 times or so (this is random):
api_fail
As you can see, a random call to my i18n endpoint failed and I got no answer for my call to _me.

The failing API call response looks like:
response_on_fail
And here we can see that the error is the one emitted by _me that got no answer…

Any ideas of what I might be doing wrong?

Another possibility might be my i18n endpoint that is using the official elasticsearch client that might or might not restore the domains properly. Could the other endpoint mess with my _me route?

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions