Skip to content

Commit 0f24530

Browse files
committed
fix(client-runtime): walk full error chain in readTraceId to find valid trace IDs
Previously readTraceId called findErrorTraceId which returns the first non-empty traceId in an error chain without regex validation. If that first traceId failed the SAFE_TRACE_ID check, readTraceId returned undefined without continuing to search deeper causes. Now readTraceId walks the chain itself, integrating the SAFE_TRACE_ID regex into each step so it skips invalid trace IDs and continues looking for a valid one deeper in the chain.
1 parent cea50fa commit 0f24530

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

packages/client-runtime/src/errors/safeLog.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { findErrorTraceId } from "./errorTrace.ts";
2-
31
const SAFE_ERROR_LABEL =
42
/^(?:Error|EvalError|RangeError|ReferenceError|SyntaxError|TypeError|URIError|AggregateError|DOMException|[A-Za-z][A-Za-z0-9]*(?:Error|Failure))$/;
53
const SAFE_TRACE_ID = /^[A-Za-z0-9._:-]{1,128}$/;
@@ -60,8 +58,19 @@ function readErrorTag(error: unknown): string | undefined {
6058

6159
function readTraceId(error: unknown): string | undefined {
6260
try {
63-
const traceId = findErrorTraceId(error);
64-
return traceId !== null && SAFE_TRACE_ID.test(traceId) ? traceId : undefined;
61+
const seen = new Set<object>();
62+
let current: unknown = error;
63+
64+
while (typeof current === "object" && current !== null && !seen.has(current)) {
65+
seen.add(current);
66+
const record = current as { readonly cause?: unknown; readonly traceId?: unknown };
67+
if (typeof record.traceId === "string" && SAFE_TRACE_ID.test(record.traceId)) {
68+
return record.traceId;
69+
}
70+
current = record.cause;
71+
}
72+
73+
return undefined;
6574
} catch {
6675
return undefined;
6776
}

0 commit comments

Comments
 (0)