[codex] Preserve trace IDs across error causes (#3426)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
d8bf307d22
commit
b6e384fb27
2 files changed
+54
-3
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import * as Cause from "effect/Cause";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { findErrorTraceId } from "./errorTrace.ts";
|
||||
@@ -22,4 +23,22 @@ describe("findErrorTraceId", () => {
|
||||
|
||||
expect(findErrorTraceId(error)).toBeNull();
|
||||
});
|
||||
|
||||
it("finds trace metadata in Effect cause branches", () => {
|
||||
const cause = Cause.fromReasons<unknown>([
|
||||
Cause.makeFailReason(new Error("first failure")),
|
||||
Cause.makeFailReason({ traceId: "trace-secondary" }),
|
||||
]);
|
||||
|
||||
expect(findErrorTraceId(cause)).toBe("trace-secondary");
|
||||
});
|
||||
|
||||
it("finds trace metadata in aggregate error branches", () => {
|
||||
const error = new AggregateError(
|
||||
[new Error("first failure"), { traceId: "trace-aggregate" }],
|
||||
"request failed",
|
||||
);
|
||||
|
||||
expect(findErrorTraceId(error)).toBe("trace-aggregate");
|
||||
});
|
||||
});
|
||||
@@ -1,17 +1,49 @@
|
||||
import * as Cause from "effect/Cause";
|
||||
|
||||
const MAX_ERROR_TRACE_NODES = 128;
|
||||
|
||||
export function findErrorTraceId(error: unknown): string | null {
|
||||
const seen = new Set<object>();
|
||||
let current: unknown = error;
|
||||
const pending: Array<unknown> = [error];
|
||||
let inspectedNodeCount = 0;
|
||||
|
||||
while (typeof current === "object" && current !== null && !seen.has(current)) {
|
||||
while (pending.length > 0 && inspectedNodeCount < MAX_ERROR_TRACE_NODES) {
|
||||
const current = pending.pop();
|
||||
inspectedNodeCount += 1;
|
||||
if (typeof current !== "object" || current === null || seen.has(current)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(current);
|
||||
const record = current as {
|
||||
readonly cause?: unknown;
|
||||
readonly errors?: unknown;
|
||||
readonly traceId?: unknown;
|
||||
};
|
||||
if (typeof record.traceId === "string" && record.traceId.trim().length > 0) {
|
||||
return record.traceId;
|
||||
}
|
||||
current = record.cause;
|
||||
|
||||
if (Array.isArray(record.errors)) {
|
||||
for (let index = record.errors.length - 1; index >= 0; index -= 1) {
|
||||
pending.push(record.errors[index]);
|
||||
}
|
||||
}
|
||||
if (Cause.isCause(current)) {
|
||||
for (let index = current.reasons.length - 1; index >= 0; index -= 1) {
|
||||
const reason = current.reasons[index];
|
||||
switch (reason?._tag) {
|
||||
case "Fail":
|
||||
pending.push(reason.error);
|
||||
break;
|
||||
case "Die":
|
||||
pending.push(reason.defect);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ("cause" in record) {
|
||||
pending.push(record.cause);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user