why it can use without new HttpException() then trown a error I saw this method elsewhere,and it is work
Here is HttpException
class HttpException extends Error{
constructor(public status: number,public message: string){
super(message);
this.status=status;
this.message=message;
}
}
export default HttpException;
Here is error handler
const errorMiddleware = (error: HttpException, _request: Request, response: Response, next: NextFunction) => {
const status = error.status || 500;
const message = error.message || ' something wrong';
response
.status(status)
.send({
status,
message
});
logger.error(error.name);
logger.error(error.message);
logger.error(error.stack);
next(error);
};
user in app.ts
// ...some routers
app.use(errorMiddleware)
api error in postman
{
"status": 500,
"message": "User validation failed: username: Error, expected `username` to be unique. Value: `aaa111`"
}