@hapi/boom is not returning correct error as thrown in code. Here is the code sample:
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
}
verify.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
}
}
Now, on error, it should either return Unauthorized, but it is returning Internal Server Error always.
Any solution to return actual error with information?
stack:
@hapi/hapi : 20.0.3
@hapi/boom : 9.1.1
I guess it won't help you anymore, since the question is 9 months old, but in the code you posted
clientId
isundefined
in thetry
block. This means you end up in thecatch
block and (re)throw
the original error.Since you are using a regular
throw
in thecatch
block, hapi callsthrow Boom.internal()
, which is a HTTP 500 internal server error.