From 8593abd426c9cbecf23fa9ae0d0a5f2aedfa2e40 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Mon, 20 Jan 2025 20:16:44 +0400 Subject: [PATCH] Add helper to prettify exn before logging (#430) --- .../static/codegen/src/ErrorHandling.res | 36 +++++-------------- .../cli/templates/static/codegen/src/Time.res | 2 +- .../src/eventFetching/hypersync/HyperSync.res | 2 +- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/codegenerator/cli/templates/static/codegen/src/ErrorHandling.res b/codegenerator/cli/templates/static/codegen/src/ErrorHandling.res index 316160d91..2cbb24a0b 100644 --- a/codegenerator/cli/templates/static/codegen/src/ErrorHandling.res +++ b/codegenerator/cli/templates/static/codegen/src/ErrorHandling.res @@ -1,43 +1,25 @@ -type exnType = Js(Js.Exn.t) | Other(exn) +type t = {logger: Pino.t, exn: exn, msg: option} -type t = {logger: Pino.t, exn: exnType, msg: option} - -let makeExnType = (exn): exnType => { - // exn might be not an object which will break the pattern match by RE_EXN_ID - if exn->Obj.magic { - switch exn { - | Js.Exn.Error(e) => - Js(e) - | exn => Other(exn) - } - } else { - Other(exn) +let prettifyExn = exn => { + switch exn { + | Js.Exn.Error(e) => e->(Utils.magic: Js.Exn.t => exn) + | exn => exn } } let make = (exn, ~logger=Logging.logger, ~msg=?) => { - {logger, msg, exn: exn->makeExnType} + {logger, msg, exn} } let log = (self: t) => { switch self { - | {exn: Js(e), msg: Some(msg), logger} => logger->Logging.childErrorWithExn(e, msg) - | {exn: Js(e), msg: None, logger} => logger->Logging.childError(e) - | {exn: Other(e), msg: Some(msg), logger} => logger->Logging.childErrorWithExn(e, msg) - | {exn: Other(e), msg: None, logger} => logger->Logging.childError(e) - } -} - -exception JsExnError(Js.Exn.t) -let getExn = (self: t) => { - switch self.exn { - | Other(exn) => exn - | Js(e) => JsExnError(e) + | {exn, msg: Some(msg), logger} => logger->Logging.childErrorWithExn(exn->prettifyExn, msg) + | {exn, msg: None, logger} => logger->Logging.childError(exn->prettifyExn) } } let raiseExn = (self: t) => { - self->getExn->raise + self.exn->raise } let mkLogAndRaise = (~logger=?, ~msg=?, exn) => { diff --git a/codegenerator/cli/templates/static/codegen/src/Time.res b/codegenerator/cli/templates/static/codegen/src/Time.res index 014b2a90c..fecbd2bf0 100644 --- a/codegenerator/cli/templates/static/codegen/src/Time.res +++ b/codegenerator/cli/templates/static/codegen/src/Time.res @@ -16,7 +16,7 @@ let rec retryAsyncWithExponentialBackOff = async ( let nextRetryCount = retryCount + 1 logger->Logging.childWarn({ "message": `Retrying query ${nextRetryCount->Belt.Int.toString}/${maxRetries->Belt.Int.toString} in ${backOffMillis->Belt.Int.toString}ms - waiting for correct result.`, - "error": exn, + "error": exn->ErrorHandling.prettifyExn, }) await resolvePromiseAfterDelay(~delayMilliseconds=backOffMillis) diff --git a/codegenerator/cli/templates/static/codegen/src/eventFetching/hypersync/HyperSync.res b/codegenerator/cli/templates/static/codegen/src/eventFetching/hypersync/HyperSync.res index a92597157..808a2710f 100644 --- a/codegenerator/cli/templates/static/codegen/src/eventFetching/hypersync/HyperSync.res +++ b/codegenerator/cli/templates/static/codegen/src/eventFetching/hypersync/HyperSync.res @@ -214,7 +214,7 @@ module HeightQuery = { | exception e => logger->Logging.childWarn({ "message": `Failed to get height from endpoint. Retrying in ${retryIntervalMillis.contents->Int.toString}ms...`, - "error": e, + "error": e->ErrorHandling.prettifyExn, }) await Time.resolvePromiseAfterDelay(~delayMilliseconds=retryIntervalMillis.contents) retryIntervalMillis := retryIntervalMillis.contents * backOffMultiplicative