Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server throws Boom.notFound, replies with 404 to the client and in the server logs unwanted error is logged #4261

Closed
ilyaigpetrov opened this issue Jun 15, 2021 · 1 comment
Labels
support Questions, discussions, and general support

Comments

@ilyaigpetrov
Copy link

ilyaigpetrov commented Jun 15, 2021

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v16.3.0
  • module version with issue: @hapi/hapi: ^20.0.1
  • last module version without issue: unknown
  • environment (e.g. node, browser, native): node + wsl
  • used with (e.g. hapi application, another framework, standalone, ...): a web server with API and static files
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

// ...some imports...
const critical = ['error', 'critical'];
process.on('unhandledRejection', (err) => {
  server.log(critical, err);
});

process.on('uncaughtException', (err) => {
  server.log(critical, err);
});
// ...
const assertClientWasFound = (opStatus, log) => {
  if (opStatus) {
    if (opStatus.reason !== 'client_not_found') {
      log(critical, `Strange reason in getList: ${opStatus.reason}.`);
    }
    log('info', 'Not found in getList.');
    throw Boom.notFound('Client email wasn\'t found. Check that you subscribed using your credit card and this email.');
  }
};
// Then some route handler calls assertClientWasFound({/* some content */}, log); 

What was the result you got?

Server throws Boom.notFound, replies with 404 to the client and in the server logs unwanted error is logged:

Request error: unknown
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
TypeError: Cannot read property 'statusCode' of null
    at Request._finalize (/home/ilyaigpetrov/Projects/my-project/node_modules/@hapi/hapi/lib/request.js:497:31)
    at Request._reply (/home/ilyaigpetrov/Projects/my-project/node_modules/@hapi/hapi/lib/request.js:434:18)
    at Request._execute (/home/ilyaigpetrov/Projects/my-project/node_modules/@hapi/hapi/lib/request.js:280:14)    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Request info: MyExtension: userEmail: Not found in getList.
Request error: Client email wasn't found. Check that you subscribed using your credit card and this email.

What result did you expect?

Server throws Boom.notFound, replies with 404 to the client and in the server logs there is NO unwanted error. Only these lines are expected:

Request info: MyExtension: userEmail: Not found in getList.
Request error: Client email wasn't found. Check that you subscribed using your credit card and this email.
@ilyaigpetrov ilyaigpetrov added the support Questions, discussions, and general support label Jun 15, 2021
@ilyaigpetrov
Copy link
Author

ilyaigpetrov commented Jun 19, 2021

I managed to minimize the code to the following:

import Hapi from '@hapi/hapi';
console.log('='.repeat(80));
const init = async () => {
  const server = Hapi.server({
    port: 80,
    host: '0.0.0.0',
  });

server.events.on('request', function (request) {
    console.log(request.info.remoteAddress + ': ' + request.method.toUpperCase() + ' ' + request.path + ' --> ');
});

  await server.start();
  console.log(`Running on ${server.info.uri}.`);
};
init();

Also I realized by the error message that this bug is a dupe of #4208 which is for node 15.5.0 but in my case it is 16.3.0.
After upgrading @hapi/hapi from 20.0.1 to 20.1.4 solved the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

1 participant