From 15f5690c31f7239068458b783499b56921be092d Mon Sep 17 00:00:00 2001 From: Andy Richardson Date: Mon, 7 Dec 2020 11:55:31 +0000 Subject: [PATCH 1/2] Serialize error before sending to xray --- src/traceResolvers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/traceResolvers.js b/src/traceResolvers.js index 4e45c4c..e9d0afa 100644 --- a/src/traceResolvers.js +++ b/src/traceResolvers.js @@ -27,7 +27,7 @@ const tracer = function (resolver, parent, args, ctx, info) { result.then(function () { subsegment.close(); }).catch(function (error) { - subsegment.close(error); + subsegment.close(error.message); }); } else { subsegment.close(); From 10e14c6fc1954b5ba3633e2174ee72697b50194e Mon Sep 17 00:00:00 2001 From: Andy Richardson Date: Mon, 7 Dec 2020 14:25:39 +0000 Subject: [PATCH 2/2] Update traceResolvers.js --- src/traceResolvers.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/traceResolvers.js b/src/traceResolvers.js index e9d0afa..e6313c6 100644 --- a/src/traceResolvers.js +++ b/src/traceResolvers.js @@ -27,7 +27,7 @@ const tracer = function (resolver, parent, args, ctx, info) { result.then(function () { subsegment.close(); }).catch(function (error) { - subsegment.close(error.message); + subsegment.close(serializeError(error)); }); } else { subsegment.close(); @@ -38,6 +38,21 @@ const tracer = function (resolver, parent, args, ctx, info) { return result; }; +/* Constructs an xray-safe error object (no circular structure errors) */ +export const serializeError = (e: string | Error) => { + if (typeof e === 'string') { + return e; + } + + // See here for how error properties are parsed - https://github.com/aws/aws-xray-sdk-node/blob/master/packages/core/lib/segments/attributes/captured_exception.js#L12 + return { + message: e.message, + name: e.name, + stack: e.stack, + }; +}; + + module.exports = function (schema) { applyMiddlewareToDeclaredResolvers(schema, tracer); };