Correct boom error not being returned in hapijs v17

255 views Asked by At

API controller:

export const addPaymentCard = async (request: ICustomerAddPaymentCardRequest, h: ResponseToolkit) => {
   try {
      const { _id: customerId } = request.auth.credentials
      const { cardToken } = request.payload

      const customerInfo: ICustomer | null = await Customer.findOne({ _id: customerId })
      if (!customerInfo) throw Boom.notFound("Customer not found")

      if (!customerInfo.stripeId) {
         const stripeCustomer = await createStripeCustomer({ email: customerInfo.email, metadata: { customerId: customerId.toString() } })
         await Customer.findOneAndUpdate({ _id: customerInfo._id }, { stripeId: stripeCustomer.id }, { new: true })
         customerInfo["stripeId"] = stripeCustomer.id
      }
      const card = await createStripeSource(customerInfo.stripeId, { source: cardToken })

      return h.response({ statusCode: 200, message: "Success", data: card })
   } catch (error) {
      logger.error(`${request.method.toUpperCase()} ${request.url.pathname}, error=${error}`)
      throw error
   }
}

Boom error is thrown in this method:

export const createStripeSource = async (customerId: string, data: Stripe.CustomerSourceCreateParams) => {
   try {
      const source = await stripe.customers.createSource(customerId, data)

      return source
   } catch (error) {
      logger.error(`create stripe payment methods incurred error=${error}`)
      throw error
   }
}

Expected error to be sent: like invalid card

Actual error being sent: Internal Server error

How can I get actual error returned by stripe? This is also happening with other methods that are returning error when called inside any handler.

node v12

@hapi/hapi v20

@hapi/boom v9

0

There are 0 answers