node.js - @hapi/boom 没有返回正确的错误,而是“500:内部服务器错误”
问题描述
@hapi/boom 没有返回代码中抛出的正确错误。这是代码示例:
控制器.ts
async (request: IRequest, h: IResponse) => {
.... // some logic
const userInfo = await verify(email, authToken)
if (!userInfo) throw Boom.unauthorized(`Unable to fetch user information`)
.... some logic
return h.response({ statusCode: 200, message: "Success", data })
} catch (error) {
throw error
}
验证.ts
export async function verify(userEmail: string, token: string) {
try {
const ticket = await client.verifyIdToken({
idToken: token,
audience: clientId
})
const payload = ticket.getPayload()
if (!payload) throw Boom.unauthorized("Google authentication failed")
const { sub: userId, name, email, aud, picture: profilePhoto } = payload
if (!aud || aud !== clientId) throw Boom.unauthorized(`Invalid token for ${config.get("appName")}`)
if (!email || email !== userEmail) throw Boom.unauthorized(`Invalid token for email ${userEmail}`)
return { userId, name, profilePhoto, email }
} catch (error) {
logger.error(error)
throw error
}
}
现在,发生错误时,它应该返回Unauthorized,但它总是返回Internal Server Error。
有什么解决方案可以返回带有信息的实际错误?
堆:
@hapi/hapi:20.0.3
@hapi/繁荣:9.1.1
解决方案
我想它不再对你有帮助了,因为这个问题已经有 9 个月了,但是在你发布的代码中clientId
是undefined
在try
块中。这意味着您最终会陷入catch
困境并(重新)throw
原始错误。
throw
由于您在块中使用常规catch
, hapi calls throw Boom.internal()
,这是一个 HTTP 500 内部服务器错误。
推荐阅读
- ios - 长度为 163 的 FCM 令牌的 Firebase 通知未到达 iOS
- shopify - Shopify Liquid - 一些标签过滤的博客未显示
- api - 如果 LCC (Pyton) 包含在最便宜的日期搜索 API 中
- python - Telethon 如何在异步方法中打印错误消息?
- javascript - 有没有办法找出 Chrome 中哪些变量改变了值?
- json - 更新 JSON 对象列表中的 JSON 对象
- cobol - 如何将长字符串移动到变量 cobol
- javascript - 在 A/B 测试工具中复制到剪贴板
- php - Json API 速率限制
- database - 从当前日期以及未来生效日期在sql中提取最近3年的记录