Skip to content

Commit

Permalink
Add helper to prettify exn before logging (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh authored Jan 20, 2025
1 parent 6a77ddc commit 8593abd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
36 changes: 9 additions & 27 deletions codegenerator/cli/templates/static/codegen/src/ErrorHandling.res
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
type exnType = Js(Js.Exn.t) | Other(exn)
type t = {logger: Pino.t, exn: exn, msg: option<string>}

type t = {logger: Pino.t, exn: exnType, msg: option<string>}

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) => {
Expand Down
2 changes: 1 addition & 1 deletion codegenerator/cli/templates/static/codegen/src/Time.res
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8593abd

Please sign in to comment.