From 107bff81ba135620dd55b2c3e6e6c542a80650d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jerna=C5=9B?= Date: Thu, 16 Jan 2025 11:27:53 +0100 Subject: [PATCH] chore(instrumentation-knex): Simplify exception parsing --- .../src/instrumentation.ts | 4 +-- .../src/utils.ts | 32 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts index ca144b2751..5b7086895d 100644 --- a/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts @@ -185,8 +185,8 @@ export class KnexInstrumentation extends InstrumentationBase { @@ -43,22 +40,27 @@ export const getFormatter = (runner: any) => { return () => ''; }; -export const cloneErrorWithNewMessage = (err: Exception, message: string) => { - if (err && err instanceof Error) { - const clonedError = Object.assign({}, err); - clonedError.message = message; - clonedError.code = err.code; - clonedError.stack = err.stack; - clonedError.errno = err.errno; - return clonedError; +export function otelExceptionFromKnexError( + err: KnexError, + message: string +): Exception { + if (!(err && err instanceof Error)) { + return err; } - return err; -}; + + return { + message, + code: err.code, + stack: err.stack, + name: err.name, + }; +} const systemMap = new Map([ ['sqlite3', DBSYSTEMVALUES_SQLITE], ['pg', DBSYSTEMVALUES_POSTGRESQL], ]); + export const mapSystem = (knexSystem: string) => { return systemMap.get(knexSystem) || knexSystem; };