我有以下api端点
app.get('/api/status', verifyUser, async (req, res) => {
try {
// Something
} catch (error) {
return res.status(200).send({})
}
})
在这里,如果出现错误或用户未通过身份验证,我需要传递一个空对象
在那条路线中,我有一个verifyUser
中间件,它只是检查用户是否已通过身份验证的这段代码。
const verifyUser = async (req, res, next) => {
const token = req.cookies.userId
if(token) {
try {
const tokenVerficiation = await verifyToken(token)
res.locals.userId = tokenVerficiation.userId
next()
} catch (error) {
console.error(error)
throw new Error(ErrorTypes.NOT_AUTHORIZED)
}
} else {
throw new Error(ErrorTypes.NOT_AUTHORIZED)
}
}
现在,当我到达端点时,我希望它能够返回响应,但不幸的是,我在控制台中收到此错误
UnhandledPromiseRejectionWarning: Error: 401/Not-authorized
at verifyUser (/Users/a/Desktop/cat/server/src/auth/auth.js:127:13)
at Layer.handle [as handle_request] (/Users/a/Desktop/cat/server/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/a/Desktop/cat/server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/a/Desktop/cat/server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/a/Desktop/cat/server/node_modules/express/lib/router/layer.js:95:5)
at /Users/a/Desktop/cat/server/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/a/Desktop/cat/server/node_modules/express/lib/router/index.js:335:12)
at next (/Users/a/Desktop/cat/server/node_modules/express/lib/router/index.js:275:10)
at responseTime (/Users/a/Desktop/cat/server/node_modules/response-time/index.js:61:5)
at Layer.handle [as handle_request] (/Users/a/Desktop/cat/server/node_modules/express/lib/router/layer.js:95:5)
(node:26895) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:26895) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:26895) UnhandledPromiseRejectionWarning: Error: 401/Not-authorized
at verifyUser (/Users/a/Desktop/cat/server/src/auth/auth.js:127:13)
at Layer.handle [as handle_request] (/Users/a/Desktop/cat/server/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/a/Desktop/cat/server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/a/Desktop/cat/server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/a/Desktop/cat/server/node_modules/express/lib/router/layer.js:95:5)
at /Users/a/Desktop/cat/server/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/a/Desktop/cat/server/node_modules/express/lib/router/index.js:335:12)
at next (/Users/a/Desktop/cat/server/node_modules/express/lib/router/index.js:275:10)
at responseTime (/Users/a/Desktop/cat/server/node_modules/response-time/index.js:61:5)
at Layer.handle [as handle_request] (/Users/a/Desktop/cat/server/node_modules/express/lib/router/layer.js:95:5)
(node:26895) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:26895) UnhandledPromiseRejectionWarning: Error: 401/Not-authorized
at verifyUser (/Users/a/Desktop/cat/server/src/auth/auth.js:127:13)
at Layer.handle [as handle_request] (/Users/a/Desktop/cat/server/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/a/Desktop/cat/server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/a/Desktop/cat/server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/a/Desktop/cat/server/node_modules/express/lib/router/layer.js:95:5)
at /Users/a/Desktop/cat/server/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/a/Desktop/cat/server/node_modules/express/lib/router/index.js:335:12)
at next (/Users/a/Desktop/cat/server/node_modules/express/lib/router/index.js:275:10)
at responseTime (/Users/a/Desktop/cat/server/node_modules/response-time/index.js:61:5)
at Layer.handle [as handle_request] (/Users/a/Desktop/cat/server/node_modules/express/lib/router/layer.js:95:5)
(node:26895) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
有人可以为我提供错误原因或错误修复方法吗?
答案 0 :(得分:0)
您的中间件似乎工作正常。 verifyUser
首先执行,然后执行
也许您可以添加更多日志记录以查看原因
const verifyUser = async (req, res, next) => {
// ...
if(token) {
try {
// ...
} catch (error) {
console.error('Unexpected error')
throw new Error(ErrorTypes.NOT_AUTHORIZED)
}
} else {
console.error('Missing token')
throw new Error(ErrorTypes.NOT_AUTHORIZED)
}
}
答案 1 :(得分:0)
中间件可能在到达api之前运行。这就是为什么它抛出。
app.get('/api/status', verifyUser, async (req, res) => {
try {
// Something
} catch (error) {
// This will only catch errors in the "Something"-block, not in the middleware
return res.status(200).send({})
}
})
如果您不希望在用户未登录时抛出应用程序,则应在中间件中返回空对象,而不是抛出错误。
答案 2 :(得分:0)
const verifyUser = async (req, res, next) => {
// code which throws the error
作为异步函数,verifyUser
返回一个承诺,如果在try / catch构造的catch块中(重新)抛出错误,或者在{{1 }}。
由于没有用于捕获被拒绝承诺的处理程序(由Express提供),因此会产生未捕获的承诺拒绝错误,如该帖子中所述。
为防止发生未捕获的拒绝错误,请捕获req.cookies.userId
中引发的异常,并且不要为丢失的令牌抛出错误。
对于可能的解决方案,您可以设置标记属性verifyUser
来指示授权已成功验证:
res.locals
要在下一个中间件功能中进行测试,或者可以选择最适合您的情况 res.locals.validToken = true; // or false
...
next();
终止请求。