Errors handling

Node.js v13.5+ new argument params help you trace warnings and errors

  • --trace-uncaught

  • --trace-warnings

Example, node --trace-uncaught server.js

Error handling

Only async route functions and middlewares are handled

app.setErrorHandler(
  (err: Error, req: HttpRequest, res: HttpResponse): HttpResponse => {
    if (checkSomething(err)) {
      return res.send({
        status: 'error',
        status_code: 500,
        message: 'oops'
      })
    }
  }
);

GET http://localhost:8000/error-route

Path Parameters

NameTypeDescription

string

Not found handler

app.setNotFoundHandler((req: HttpRequest, res: HttpResponse): HttpResponse => {
    return res.send({ code: 404, message: 'You entered wrong url' });
});

GET http://localhost:8000/anyUrl

Path Parameters

NameTypeDescription

string

Validation error handler

Validation error handler unavailable for pro-slim version

app.setValidationErrorHandler((errors: AjvValidationErrors[], req: HttpRequest, res: HttpResponse): HttpResponse => {
    return res.json({ errors });
});

GET http://locahost:8000/validate-error

Request Body

NameTypeDescription

foo

string

bar

{
  "errors": {
    "type": "errors",
    "errors": [
      {
        "body": ["type should be string, but got ..."]
      }
    ]
  }
}

Last updated