From ceb99d073a3cb1d1b63b3c5dffd193c7664d75eb Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Fri, 14 Feb 2025 13:50:52 +0100 Subject: [PATCH] fix(medusa): Re throw error in instrumentation after reporting it (#11464) FIXES FRMW-2914 **What** Currently, when the instrumentation is enabled, some instrument catches the errors in order to set the span status and info, unfortunately, these errors are not re throw leading to swallow them and return nothing in the end leading to potential breaks in flows --- .changeset/wicked-roses-appear.md | 5 ++ packages/medusa/src/instrumentation/index.ts | 54 ++++++++++++-------- 2 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 .changeset/wicked-roses-appear.md diff --git a/.changeset/wicked-roses-appear.md b/.changeset/wicked-roses-appear.md new file mode 100644 index 0000000000..26a9860084 --- /dev/null +++ b/.changeset/wicked-roses-appear.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Re throw error in instrumentation after reporting it diff --git a/packages/medusa/src/instrumentation/index.ts b/packages/medusa/src/instrumentation/index.ts index 3a5344f16b..0a018cdf94 100644 --- a/packages/medusa/src/instrumentation/index.ts +++ b/packages/medusa/src/instrumentation/index.ts @@ -148,14 +148,17 @@ export function instrumentRemoteQuery() { span.setAttributes({ "query.fields": queryOptions.fields, }) - return await queryFn() - .catch((error) => { - span.setStatus({ - code: SpanStatusCode.ERROR, - message: error.message, - }) + try { + return await queryFn() + } catch (err) { + span.setStatus({ + code: SpanStatusCode.ERROR, + message: err.message, }) - .finally(() => span.end()) + throw err + } finally { + span.end() + } } ) }) @@ -176,14 +179,18 @@ export function instrumentRemoteQuery() { span.setAttributes({ "query.fields": "fields" in queryOptions ? queryOptions.fields : [], }) - return await queryFn() - .catch((error) => { - span.setStatus({ - code: SpanStatusCode.ERROR, - message: error.message, - }) + + try { + return await queryFn() + } catch (error) { + span.setStatus({ + code: SpanStatusCode.ERROR, + message: error.message, }) - .finally(() => span.end()) + throw error + } finally { + span.end() + } } ) }) @@ -201,14 +208,18 @@ export function instrumentRemoteQuery() { "fetch.select": options.select, "fetch.relations": options.relations, }) - return await fetchFn() - .catch((error) => { - span.setStatus({ - code: SpanStatusCode.ERROR, - message: error.message, - }) + + try { + return await fetchFn() + } catch (error) { + span.setStatus({ + code: SpanStatusCode.ERROR, + message: error.message, }) - .finally(() => span.end()) + throw error + } finally { + span.end() + } } ) }) @@ -251,6 +262,7 @@ export function instrumentWorkflows() { span.setAttribute(`workflow.step.${key}`, value) }) + // TODO: should we report error and re throw it? return await stepHandler().finally(() => span.end()) } )