Skip to content

Commit 4cbe944

Browse files
committed
Keep healthy data when an array element, Date, or thrown value misbehaves
- Read array elements one at a time so a throwing index getter drops only that element. - Serialize a Date with an overridden toJSON inside prune, where a throw drops only that value. - Format plugin errors without throwing (a thrown Symbol made the catch handler itself throw).
1 parent a551e88 commit 4cbe944

4 files changed

Lines changed: 85 additions & 3 deletions

File tree

‎packages/core/src/Utils.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,31 @@ export function prune(value: unknown, depth: number = 10): unknown {
484484
if (Array.isArray(normalizedValue)) {
485485
// Treat an object inside of an array as a single level
486486
const depth: number = parentIsArray ? currentDepth + 1 : currentDepth;
487-
return normalizedValue.map((e) => pruneImpl(e, maxDepth, depth, ancestors, true));
487+
// Read each element separately so a throwing index getter only drops that element.
488+
const array = normalizedValue as unknown[];
489+
const result: unknown[] = [];
490+
for (let index = 0; index < array.length; index++) {
491+
let element: unknown;
492+
try {
493+
element = array[index];
494+
} catch {
495+
result[index] = undefined;
496+
continue;
497+
}
498+
499+
result[index] = pruneImpl(element, maxDepth, depth, ancestors, true);
500+
}
501+
502+
return result;
488503
}
489504

490505
if (normalizedValue instanceof Date) {
491-
return normalizedValue;
506+
if (normalizedValue.toJSON === Date.prototype.toJSON && normalizedValue.toISOString === Date.prototype.toISOString) {
507+
return normalizedValue;
508+
}
509+
510+
// A Date with an overridden toJSON is serialized here, where a throw only drops this value.
511+
return normalizedValue.toJSON();
492512
}
493513

494514
const keys = new Set<PropertyKey>([...Object.getOwnPropertyNames(normalizedValue), ...Object.getOwnPropertySymbols(normalizedValue)]);

‎packages/core/src/plugins/EventPluginManager.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,10 @@ export class EventPluginManager {
6767
}
6868

6969
function getErrorMessage(ex: unknown): string {
70-
return ex instanceof Error ? ex.message : ex + "";
70+
// Formatting must never throw (for example a thrown Symbol, or a revoked Proxy), or the event would still be lost.
71+
try {
72+
return ex instanceof Error ? String(ex.message) : String(ex);
73+
} catch {
74+
return "Unknown error";
75+
}
7176
}

‎packages/core/test/Utils.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,44 @@ describe("Utils", () => {
11751175
);
11761176
});
11771177

1178+
test("should keep healthy array elements when one index getter throws", () => {
1179+
const array = [1, 2, 3];
1180+
Object.defineProperty(array, 1, {
1181+
enumerable: true,
1182+
get(): number {
1183+
throw new Error("getter failed");
1184+
}
1185+
});
1186+
1187+
expect(
1188+
stringify({
1189+
array
1190+
})
1191+
).toBe(
1192+
JSON.stringify({
1193+
array: [1, null, 3]
1194+
})
1195+
);
1196+
});
1197+
1198+
test("should drop a date whose toJSON throws", () => {
1199+
const date = new Date(0);
1200+
date.toJSON = () => {
1201+
throw new Error("toJSON failed");
1202+
};
1203+
1204+
expect(
1205+
stringify({
1206+
ok: true,
1207+
date
1208+
})
1209+
).toBe(
1210+
JSON.stringify({
1211+
ok: true
1212+
})
1213+
);
1214+
});
1215+
11781216
test("should serialize a revoked proxy as undefined", () => {
11791217
const { proxy, revoke } = Proxy.revocable({}, {});
11801218
revoke();

‎packages/core/test/plugins/EventPluginManager.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,25 @@ describe("EventPluginManager", () => {
119119
expect(logError).toHaveBeenCalledWith(expect.stringContaining("Async Error"));
120120
});
121121

122+
test("throwing a symbol should log and continue with the event.", async () => {
123+
const { client, logError } = createClient();
124+
const context = new EventPluginContext(client, {}, new EventContext());
125+
126+
client.config.addPlugin("1", 1, () => {
127+
// eslint-disable-next-line @typescript-eslint/only-throw-error
128+
throw Symbol("failure");
129+
});
130+
client.config.addPlugin("2", 2, (ctx: EventPluginContext) => {
131+
ctx.event.source = "plugin 2";
132+
return Promise.resolve();
133+
});
134+
135+
await EventPluginManager.run(context);
136+
expect(context.cancelled).toBe(false);
137+
expect(context.event.source).toBe("plugin 2");
138+
expect(logError).toHaveBeenCalledWith(expect.stringContaining("Symbol(failure)"));
139+
});
140+
122141
test("plugin that cancels before throwing should keep the event cancelled.", async () => {
123142
const { client } = createClient();
124143
const context = new EventPluginContext(client, {}, new EventContext());

0 commit comments

Comments
 (0)