【问题标题】:@hapi/boom not returning correct error, instead "500 : Internal Server Error"@hapi/boom 没有返回正确的错误,而是“500:内部服务器错误”
【发布时间】:2021-08-17 14:08:18
【问题描述】:

@hapi/boom 没有返回代码中抛出的正确错误。 这是代码示例:

controller.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/boom:9.1.1

【问题讨论】:

  • Error 500 字面意思是服务器出现错误。

标签: node.js error-handling google-authentication hapi google-auth-library-nodejs


【解决方案1】:

我想它对你没有帮助了,因为这个问题已经 9 个月了,但是在你发布的代码中 clientIdundefinedtry 块中。这意味着您最终会进入 catch 块和(重新)throw 原始错误。

由于您在 catch 块中使用常规 throw,因此 hapi 调用 throw Boom.internal(),这是一个 HTTP 500 内部服务器错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多