【发布时间】:2022-01-17 12:07:36
【问题描述】:
我在 node/express/typescript 中有一个方法,像这样:
// eslint-disable-next-line
export const errorConverter = (err: any, req: any, res: any, next: any) => {
let error = err
if (!(error instanceof ApiError)) {
const statusCode =
error.statusCode || error instanceof mongoose.Error ? httpStatus.BAD_REQUEST : httpStatus.INTERNAL_SERVER_ERROR
const message = error.message || httpStatus[statusCode]
error = new ApiError(statusCode, message as string, true, err.stack)
}
next(error)
}
并像这样使用它app.use(errorConverter)。
如何避免在参数中使用any,我可以在那里使用什么类型。需要一些帮助。
【问题讨论】:
-
你可以使用
unknown -
@about14sheep,如果我给出错误:未知,在 error.statusCode 中出现问题
标签: node.js typescript express