nanoexpress / legacy

[Deprecated] Nano-framework for Node.js. Use PRO version
https://nanoexpress.js.org
Apache License 2.0
107 stars 13 forks source link

UnhandledPromiseRejectionWarning and app.setValidationErrorHandler() #68

Closed KentonParton closed 4 years ago

KentonParton commented 4 years ago

What version of nanoexpress are you using? 1.1.12

Context I am wanting to validate the structure of the request that is being send to "/uploadSchema". After the validation errors are returned, an "UnhandledPromiseRejectionWarning" is showing in the terminal. The structure of the request is incorrect deliberately.

Request being sent

{
    "schema": {},
    "key": {} // uploadSchema expects a string
}

Expected request structure

{
    "schema": {},
    "key": "someSchemaName"
}

Your code

const nanoexpress = require('nanoexpress');
const app = nanoexpress();
const Ajv = require('ajv');
const ajv = new Ajv({
  validateSchema: false,
  allErrors: true,
  jsonPointers: false,
  ownProperties: true
});

app.post('/uploadSchema', {
    schema: {
      body: {
        type: 'object',
        properties: {
          schema: { type: 'object' },
          key: { type: 'string' },
        },
        required: ["schema", "key"]
      },
      response: false,
      query: false,
      params: false,
      cookies: false
    }
  },
  async (req, res) => {
    res.send({status: 200})
  });

app.setValidationErrorHandler((errors, req, res) => {
  res.end('Validation errors, ' + JSON.stringify(errors));
});

app.listen(4000);
module.exports = app;

Response

Validation errors, {"type":"errors","errors":[{"type":"body","messages":["should be string"]}]}

After a few seconds the following was displayed in the terminal:

(node:16302) UnhandledPromiseRejectionWarning: Invalid access of discarded (invalid, deleted) uWS.HttpResponse/SSLHttpResponse.
(node:16302) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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(). (rejection id: 2)
(node:16302) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:16302) UnhandledPromiseRejectionWarning: Invalid access of discarded (invalid, deleted) uWS.HttpResponse/SSLHttpResponse.
(node:16302) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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(). (rejection id: 4)

What results did you expect? I expected there to be no UnhandledPromiseRejectionWarning. I am not sure if this is due to a mistake on my end; however, I see that a recent issue was closed which I believe covers the same issue.

dalisoft commented 4 years ago

Hi @KentonParton.

Thanks for reporting bug and for trying out nanoexpress.

Here are some tips for you:

  1. Always return HttpResponse from Validation handler for avoiding like these issues
  2. Returning nanoexpress instance does not required unless you are doing something with that instance
dalisoft commented 4 years ago

@KentonParton Can you check update, it is fixes your application issue?

KentonParton commented 4 years ago

@dalisoft Thank you for your quick response. The update solved this issue.

dalisoft commented 4 years ago

Thank you for confirm. Have a nice day!