tywalch/electrodb

How to handle electrodb errores gracefully inside a lambda function.

Easy-Cloud-in opened this issue · 1 comments

Hi, i have lambda function like this

export async function deleteSubcription(event: any) {
  const sub = paramValidation.safeParse(event.body.params);
  if (!sub.success) {
    throw createError(422, "Invalid input parameters!");
  } 
  try {
    let response = await SubscriptionEntity.delete({
      subId: sub.data.subId,
      planId: sub.data.planId,
    })
      .where((attr, op) => `${op.lte(attr.ref_count, 0)}`)
      .go();
    return {
      statusCode: "200",
      body: JSON.stringify({
        message: response,
        error: "",
      }),
    };
  } catch (error: any) {
    return {
      statusCode: error.statusCode,
      body: JSON.stringify({
        message: "",
        error: error,
      }),
    };
  }
}
export const handler = commonMiddy(deleteSubcription);

Electrodb throws an error but i am unable to catch it in my function code?
Any think i have to configure to catch errors ?

sam3d commented

It seems like the issue here is probably that the error you're catching is an ElectroDB error, but you're checking for statusCode (and in general relying on the error being a different type than what it actually is). What behaviour are you trying to achieve?