node-decorators
node-decorators copied to clipboard
[BUG] Global middleware's use method is not being called
Describe the bug
The use
method of my class that implements the ErrorMiddleware isn't being called.
@Injectable()
export class GlobalErrorMiddleware implements ErrorMiddleware {
public use(
e: Error,
request: Request,
response: Response,
next: NextFunction
) {
console.error('Global error middleware')
return response.status(500).json({
error: {
code: "INTERNAL_SERVER_ERROR",
message: "Something went wrong",
},
metadata: {
statusCode: 500,
resource: request.url,
timestamp: new Date().toString(),
},
});
}
}
It's important to note that I have a catch-all middleware that comes after the middleware line.
const container = new Container();
container.provide([
{
provide: ERROR_MIDDLEWARE,
useClass: GlobalErrorMiddleware,
},
]);
app.use((request: ExpressRequest, response) => {
return response.status(404).json({
error: {
code: "RouteNotFound",
message: "Route Not Found",
},
metadata: {
statusCode: 404,
resource: request.url,
timestamp: new Date().toString(),
},
});
});
But despite that I'd expect the use
method gets called.
To Reproduce
Please visit the repository here: https://github.com/d30jeff/decorators-express-middleware-issue
yarn && yarn start admin:dev
,
Please navigate to http://localhost:3000/v1/cats
Expected behavior
As per the middleware's use
method, I'm expecting the output of console.error('Global error middleware')
to be displayed in the terminal.
Screenshots
Server (please complete the following information):
- OS: MacOS
- NodeJS: v20.9.0
Package version
"@decorators/di": "^3.1.0",
"@decorators/express": "^3.0.0",