From b6a1f0d2cac10ad66cd794f3797968b9821c3540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20=C5=BBuraw?= <9116238+krzysztofzuraw@users.noreply.github.com> Date: Fri, 27 Sep 2024 12:07:04 +0200 Subject: [PATCH] try with if --- packages/logger/src/logger-otel-transport.ts | 44 ++++++++++---------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/logger/src/logger-otel-transport.ts b/packages/logger/src/logger-otel-transport.ts index 0243c52b8..464f830ab 100644 --- a/packages/logger/src/logger-otel-transport.ts +++ b/packages/logger/src/logger-otel-transport.ts @@ -42,28 +42,30 @@ export const attachLoggerOtelTransport = ( {} as Record, ); - /** - * Try to serialize Error. Modern-errors has plugin to serialize - * https://github.com/ehmicky/modern-errors-serialize - * - * It add "serialize" method that converts class to plain object, working for OTEL. - * - * This is not perfect, doesn't work for nested object. We probably need to introduce some abstraction - * on logger error? - */ - try { - const errorAttribute = serializedAttributes.error; - const ErrorConstructor = errorAttribute["constructor"]; + if (serializedAttributes.error) { + /** + * Try to serialize Error. Modern-errors has plugin to serialize + * https://github.com/ehmicky/modern-errors-serialize + * + * It add "serialize" method that converts class to plain object, working for OTEL. + * + * This is not perfect, doesn't work for nested object. We probably need to introduce some abstraction + * on logger error? + */ + try { + const errorAttribute = serializedAttributes.error; + const ErrorConstructor = errorAttribute["constructor"]; - console.log("ErrorConstructor", JSON.stringify(serializedAttributes.error)); - // @ts-expect-error - ErrorConstructor is a class that could have serialize method. If not, safely throw and ignore - serializedAttributes.error = ErrorConstructor.serialize(serializedAttributes.error, { - exclude: ["errors"], - }); - // @ts-expect-error - Additional mapping for Datadog - serializedAttributes.error.type = serializedAttributes.error.name; - } catch (e) { - console.error("Error serialization failed", JSON.stringify(e)); + console.log("ErrorConstructor", JSON.stringify(serializedAttributes.error)); + // @ts-expect-error - ErrorConstructor is a class that could have serialize method. If not, safely throw and ignore + serializedAttributes.error = ErrorConstructor.serialize(serializedAttributes.error, { + exclude: ["errors"], + }); + // @ts-expect-error - Additional mapping for Datadog + serializedAttributes.error.type = serializedAttributes.error.name; + } catch (e) { + console.error("Error serialization failed", JSON.stringify(e)); + } } logs.getLogger("app-logger-otel").emit({