lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.42k stars 489 forks source link

Errors thrown in custom middleware do not get passed to error handler #1504

Closed DaWoCX closed 8 months ago

DaWoCX commented 9 months ago

Error thrown in custom middleware are not caught or handled by the provided error handler

Sorting

Expected Behavior

I have a custom middleware which authenticates a request based on some content in the request header. If the request is not valid I throw an custom AUTHENTCATION_ERROR. I Would expect that the error would be caught in the error handler added to express by using "app.use(...)" like shown in the tsoa documentation.

I would expect the error handler to catch those errors just like the errors were caught using the tsoa expressAuthentication function / @Security decorator.

export async function customRequestAuthenticator(req, res, next) {
  const authHeader = req.header("Authorization");
  if (authHeader) {
    const token = authHeader.split(" ")[1];
    if (await verifyToken(token)) {
      next();
    }
  }
  throw new AUTHENTICATION_ERROR();
}

Current Behavior

Currently the error is not caught in the error handler. It just is thrown as expected and the app crashes.

Possible Solution

Steps to Reproduce

Context (Environment)

Version of the library: 5.1.1 Version of NodeJS: 20.9.0

github-actions[bot] commented 9 months ago

Hello there DaWoCX 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

github-actions[bot] commented 8 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days