From feb232de6d8222bfb8c122200334557c0c3db230 Mon Sep 17 00:00:00 2001 From: Joey Zhou Date: Thu, 3 Oct 2024 14:16:52 -0700 Subject: [PATCH 1/2] fix: error log format --- src/db/tables/subscriber_email_preferences.ts | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/db/tables/subscriber_email_preferences.ts b/src/db/tables/subscriber_email_preferences.ts index 3f69f758028..6ed7f0789f1 100644 --- a/src/db/tables/subscriber_email_preferences.ts +++ b/src/db/tables/subscriber_email_preferences.ts @@ -62,9 +62,16 @@ async function addEmailPreferenceForSubscriber( .returning("*"); logger.debug("add_email_preference_for_subscriber_success"); } catch (e) { - logger.error("error_add_subscriber_email_preference", { - exception: e as string, - }); + if (e instanceof Error) { + logger.error("error_add_subscriber_email_preference", { + message: e.message, + stack_trace: e.stack, + }); + } else { + logger.error("error_add_subscriber_email_preference", { + error: String(e), + }); + } throw e; } @@ -119,7 +126,8 @@ async function updateEmailPreferenceForSubscriber( logger.debug("update_email_preference_for_subscriber_success"); } catch (e) { logger.error("error_update_subscriber_email_preference", { - exception: e as string, + message: (e as Error).message, + stack_trace: (e as Error).stack, }); throw e; @@ -161,7 +169,8 @@ async function getEmailPreferenceForSubscriber(subscriberId: number) { ); } catch (e) { logger.error("error_get_subscriber_email_preference", { - exception: e as string, + message: (e as Error).message, + stack_trace: (e as Error).stack, }); throw e; @@ -195,7 +204,8 @@ async function getEmailPreferenceForUnsubscribeToken(unsubscribeToken: string) { logger.error( "error_get_subscriber_email_preference_for_unsubscribe_token", { - exception: e as string, + message: (e as Error).message, + stack_trace: (e as Error).stack, }, ); @@ -230,7 +240,8 @@ async function unsubscribeMonthlyMonitorReportForUnsubscribeToken( logger.error( "error_unsubscribe_monthly_monitor_report_for_unsubscribe_token", { - exception: e, + message: (e as Error).message, + stack_trace: (e as Error).stack, }, ); captureException(e); @@ -274,7 +285,8 @@ async function getEmailPreferenceForPrimaryEmail(email: string) { ); } catch (e) { logger.error("error_get_subscriber_email_preference_for_primary_email", { - exception: e as string, + message: (e as Error).message, + stack_trace: (e as Error).stack, }); throw e; From bd2c5ccc27a1ed08c52751b5e14571d6104ed234 Mon Sep 17 00:00:00 2001 From: Joey Zhou Date: Thu, 3 Oct 2024 14:22:28 -0700 Subject: [PATCH 2/2] fix: refactor --- src/db/tables/subscriber_email_preferences.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/db/tables/subscriber_email_preferences.ts b/src/db/tables/subscriber_email_preferences.ts index 6ed7f0789f1..9c932a4b4dc 100644 --- a/src/db/tables/subscriber_email_preferences.ts +++ b/src/db/tables/subscriber_email_preferences.ts @@ -62,16 +62,10 @@ async function addEmailPreferenceForSubscriber( .returning("*"); logger.debug("add_email_preference_for_subscriber_success"); } catch (e) { - if (e instanceof Error) { - logger.error("error_add_subscriber_email_preference", { - message: e.message, - stack_trace: e.stack, - }); - } else { - logger.error("error_add_subscriber_email_preference", { - error: String(e), - }); - } + logger.error("error_add_subscriber_email_preference", { + message: (e as Error).message, + stack_trace: (e as Error).stack, + }); throw e; }