Skip to content

Commit aefc383

Browse files
committed
✨ feat: Preserve AggregateError.errors even if showHidden is "none"
1 parent f3427e6 commit aefc383

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

packages/lite/src/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -737,13 +737,18 @@ function buildTree(
737737
)
738738
otherKeys.push(key);
739739
}
740-
// Preserve Error.cause
741-
if (
742-
value instanceof Error &&
743-
allKeys.indexOf("cause") !== -1 &&
744-
otherKeys.indexOf("cause") === -1
745-
)
746-
otherKeys.push("cause");
740+
const ensurePreservedKeys = [
741+
// Error.cause
742+
value instanceof Error && "cause",
743+
// AggregateError.errors
744+
// @ts-expect-error - AggregateError is only available in ES2021+
745+
typeof AggregateError !== "undefined" &&
746+
// @ts-expect-error - AggregateError is only available in ES2021+
747+
value instanceof AggregateError &&
748+
"errors",
749+
].filter((k) => k) as string[];
750+
for (const key of ensurePreservedKeys)
751+
if (allKeys.indexOf(key) !== -1 && otherKeys.indexOf(key) === -1) otherKeys.push(key);
747752

748753
// Array element
749754
const arrayEntries: Node[] = arrayItemKeys.map((key) =>

packages/lite/test/error.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,17 @@ describe("Error", () => {
243243
error = e;
244244
}
245245

246-
expect(show(error)).toMatch(/^AbortError: The operation was aborted.*?\n {4}at /);
246+
expect(show(error)).toMatch(
247+
/^AbortError: The operation was aborted.*?\n {4}at .+\[cause\]: Error: boom\n/s,
248+
);
249+
expect(inspect(error)).toEqual(util.inspect(error));
250+
expect(inspect(error, { showHidden: true })).toEqual(util.inspect(error, { showHidden: true }));
251+
});
252+
253+
it("should show AggregateError.errors by default", () => {
254+
const error = new AggregateError([new Error("foo"), new TypeError("bar")], "baz");
255+
256+
expect(show(error)).toMatch(/^AggregateError: baz\n {4}at .+\[errors\]: \[/s);
247257
expect(inspect(error)).toEqual(util.inspect(error));
248258
expect(inspect(error, { showHidden: true })).toEqual(util.inspect(error, { showHidden: true }));
249259
});

src/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -887,13 +887,18 @@ function buildTree(
887887
)
888888
otherKeys.push(key);
889889
}
890-
// Preserve Error.cause
891-
if (
892-
value instanceof Error &&
893-
allKeys.indexOf("cause") !== -1 &&
894-
otherKeys.indexOf("cause") === -1
895-
)
896-
otherKeys.push("cause");
890+
const ensurePreservedKeys = [
891+
// Error.cause
892+
value instanceof Error && "cause",
893+
// AggregateError.errors
894+
// @ts-expect-error - AggregateError is only available in ES2021+
895+
typeof AggregateError !== "undefined" &&
896+
// @ts-expect-error - AggregateError is only available in ES2021+
897+
value instanceof AggregateError &&
898+
"errors",
899+
].filter((k) => k) as string[];
900+
for (const key of ensurePreservedKeys)
901+
if (allKeys.indexOf(key) !== -1 && otherKeys.indexOf(key) === -1) otherKeys.push(key);
897902

898903
// Array element
899904
const arrayEntries: Node[] = arrayItemKeys.map((key) =>

test/error.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,17 @@ describe("Error", () => {
243243
error = e;
244244
}
245245

246-
expect(show(error)).toMatch(/^AbortError: The operation was aborted.*?\n {4}at /);
246+
expect(show(error)).toMatch(
247+
/^AbortError: The operation was aborted.*?\n {4}at .+\[cause\]: Error: boom\n/s,
248+
);
249+
expect(inspect(error)).toEqual(util.inspect(error));
250+
expect(inspect(error, { showHidden: true })).toEqual(util.inspect(error, { showHidden: true }));
251+
});
252+
253+
it("should show AggregateError.errors by default", () => {
254+
const error = new AggregateError([new Error("foo"), new TypeError("bar")], "baz");
255+
256+
expect(show(error)).toMatch(/^AggregateError: baz\n {4}at .+\[errors\]: \[/s);
247257
expect(inspect(error)).toEqual(util.inspect(error));
248258
expect(inspect(error, { showHidden: true })).toEqual(util.inspect(error, { showHidden: true }));
249259
});

0 commit comments

Comments
 (0)