Skip to content

Commit 02fce10

Browse files
committed
chore(otel): update addSpanEventWithIncomingRequestData(), AddSpanEventWithOutgoingResponseDataOptions()
catch error during JSON.stringify()
1 parent 6f8c2c7 commit 02fce10

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

packages/otel/src/lib/util.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,13 @@ export function addSpanEventWithIncomingRequestData(options: AddSpanEventWithInc
354354

355355
if (query) {
356356
if (typeof query === 'object' && Object.keys(query).length) {
357-
const value = truncateString(JSON.stringify(query, null, 2))
357+
let value = 'object could not be stringified to JSON'
358+
try {
359+
value = truncateString(JSON.stringify(query, null, 2))
360+
}
361+
catch (err) {
362+
void err
363+
}
358364
Object.defineProperty(attrs, AttrNames.Http_Request_Query, {
359365
...defaultProperty,
360366
value,
@@ -363,7 +369,13 @@ export function addSpanEventWithIncomingRequestData(options: AddSpanEventWithInc
363369
}
364370

365371
if (data && Object.keys(data).length) {
366-
const value = truncateString(JSON.stringify(data, null, 2))
372+
let value = 'object could not be stringified to JSON'
373+
try {
374+
value = truncateString(JSON.stringify(data, null, 2))
375+
}
376+
catch (err) {
377+
void err
378+
}
367379
Object.defineProperty(attrs, AttrNames.Http_Request_Body, {
368380
...defaultProperty,
369381
value,
@@ -404,7 +416,12 @@ export function addSpanEventWithOutgoingResponseData(options: AddSpanEventWithOu
404416

405417
let value = ''
406418
if (typeof body === 'object') {
407-
value = truncateString(JSON.stringify(body, null, 2))
419+
try {
420+
value = truncateString(JSON.stringify(body, null, 2))
421+
}
422+
catch {
423+
value = 'object could not be stringified to JSON'
424+
}
408425
}
409426
else if (typeof body === 'string') {
410427
value = body ? truncateString(body) : ''

0 commit comments

Comments
 (0)