Skip to content

Commit 0e12153

Browse files
authored
Capture and handle errors during stringify. (#544)
1 parent 4486928 commit 0e12153

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/utils/tag-object.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ describe("tagObject", () => {
9999
["function.request.vals.1", '{"thingTwo":2}'],
100100
]);
101101
});
102+
it("handles circular objects", () => {
103+
const span = {
104+
setTag,
105+
};
106+
var obj = {
107+
key: "value",
108+
array: Array<any>(),
109+
};
110+
obj.array.push(obj);
111+
tagObject(span, "lambda_payload", { request: obj }, 1);
112+
expect(setTag.mock.calls).toEqual([
113+
["lambda_payload.request.key", "value"],
114+
["lambda_payload.request.array.0.key", "value"],
115+
["lambda_payload.request.array.0.array.0.key", "value"],
116+
["lambda_payload.request.array.0.array.0.array.0.key", "value"],
117+
]);
118+
});
102119
it("redacts common secret keys", () => {
103120
const span = {
104121
setTag,

src/utils/tag-object.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { logDebug } from "./log";
2+
13
const redactableKeys = ["authorization", "x-authorization", "password", "token"];
24

35
export function tagObject(currentSpan: any, key: string, obj: any, depth = 0, maxDepth = 10): any {
@@ -6,7 +8,13 @@ export function tagObject(currentSpan: any, key: string, obj: any, depth = 0, ma
68
return currentSpan.setTag(key, obj);
79
}
810
if (depth >= maxDepth) {
9-
const strOrUndefined = JSON.stringify(obj);
11+
let strOrUndefined;
12+
try {
13+
strOrUndefined = JSON.stringify(obj);
14+
} catch (e) {
15+
logDebug(`Unable to stringify object for tagging: ${e}`);
16+
return;
17+
}
1018
if (typeof strOrUndefined === "undefined") return;
1119
return currentSpan.setTag(key, redactVal(key, strOrUndefined.substring(0, 5000)));
1220
}

0 commit comments

Comments
 (0)