Skip to content

Commit c994176

Browse files
Better RetryErrors (#553)
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
1 parent 15e32e9 commit c994176

6 files changed

Lines changed: 329 additions & 57 deletions

File tree

packages/stack-shared/src/utils/errors.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ StackAssertionError.prototype.name = "StackAssertionError";
8282

8383
export function errorToNiceString(error: unknown): string {
8484
if (!(error instanceof Error)) return `${typeof error}<${nicify(error)}>`;
85-
let stack = error.stack ?? "";
86-
const toString = error.toString();
87-
if (!stack.startsWith(toString)) stack = `${toString}\n${stack}`; // some browsers don't include the error message in the stack, some do
88-
return `${stack} ${nicify(Object.fromEntries(Object.entries(error)), { maxDepth: 8 })}`;
85+
return nicify(error, { maxDepth: 8 });
8986
}
9087

9188

packages/stack-shared/src/utils/hashes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function hashPassword(password: string) {
1717
return await bcrypt.hash(password, salt);
1818
}
1919

20-
export async function comparePassword(password: string, hash: string) {
20+
export async function comparePassword(password: string, hash: string): Promise<boolean> {
2121
switch (await getPasswordHashAlgorithm(hash)) {
2222
case "bcrypt": {
2323
return await bcrypt.compare(password, hash);

packages/stack-shared/src/utils/results.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { wait } from "./promises";
2-
import { deindent } from "./strings";
2+
import { deindent, nicify } from "./strings";
33

44
export type Result<T, E = unknown> =
55
| {
@@ -305,7 +305,7 @@ import.meta.vitest?.test("mapResult", ({ expect }) => {
305305

306306
class RetryError extends AggregateError {
307307
constructor(public readonly errors: unknown[]) {
308-
const strings = errors.map(e => String(e));
308+
const strings = errors.map(e => nicify(e));
309309
const isAllSame = strings.length > 1 && strings.every(s => s === strings[0]);
310310
super(
311311
errors,
@@ -314,10 +314,10 @@ class RetryError extends AggregateError {
314314
315315
${isAllSame ? deindent`
316316
Attempts 1-${errors.length}:
317-
${errors[0]}
318-
` : errors.map((e, i) => deindent`
317+
${strings[0]}
318+
` : strings.map((s, i) => deindent`
319319
Attempt ${i + 1}:
320-
${e}
320+
${s}
321321
`).join("\n\n")}
322322
`,
323323
{ cause: errors[errors.length - 1] }
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
import { describe, expect, test } from "vitest";
2+
import { NicifyOptions, deindent, nicify } from "./strings";
3+
4+
describe("nicify", () => {
5+
describe("primitive values", () => {
6+
test("numbers", () => {
7+
expect(nicify(123)).toBe("123");
8+
expect(nicify(123n)).toBe("123n");
9+
});
10+
11+
test("strings", () => {
12+
expect(nicify("hello")).toBe('"hello"');
13+
});
14+
15+
test("booleans", () => {
16+
expect(nicify(true)).toBe("true");
17+
expect(nicify(false)).toBe("false");
18+
});
19+
20+
test("null and undefined", () => {
21+
expect(nicify(null)).toBe("null");
22+
expect(nicify(undefined)).toBe("undefined");
23+
});
24+
25+
test("symbols", () => {
26+
expect(nicify(Symbol("test"))).toBe("Symbol(test)");
27+
});
28+
});
29+
30+
describe("arrays", () => {
31+
test("empty array", () => {
32+
expect(nicify([])).toBe("[]");
33+
});
34+
35+
test("single-element array", () => {
36+
expect(nicify([1])).toBe("[1]");
37+
});
38+
39+
test("single-element array with long content", () => {
40+
expect(nicify(["123123123123123"])).toBe('["123123123123123"]');
41+
});
42+
43+
test("flat array", () => {
44+
expect(nicify([1, 2, 3])).toBe("[1, 2, 3]");
45+
});
46+
47+
test("longer array", () => {
48+
expect(nicify([10000, 2, 3])).toBe(deindent`
49+
[
50+
10000,
51+
2,
52+
3,
53+
]
54+
`);
55+
});
56+
57+
test("nested array", () => {
58+
expect(nicify([1, [2, 3]])).toBe(deindent`
59+
[
60+
1,
61+
[2, 3],
62+
]
63+
`);
64+
});
65+
});
66+
67+
describe("objects", () => {
68+
test("empty object", () => {
69+
expect(nicify({})).toBe("{}");
70+
});
71+
72+
test("simple object", () => {
73+
expect(nicify({ a: 1 })).toBe('{ "a": 1 }');
74+
});
75+
76+
test("multiline object", () => {
77+
expect(nicify({ a: 1, b: 2 })).toBe(deindent`
78+
{
79+
"a": 1,
80+
"b": 2,
81+
}
82+
`);
83+
});
84+
});
85+
86+
describe("custom classes", () => {
87+
test("class instance", () => {
88+
class TestClass {
89+
constructor(public value: number) {}
90+
}
91+
expect(nicify(new TestClass(42))).toBe('TestClass { "value": 42 }');
92+
});
93+
});
94+
95+
describe("built-in objects", () => {
96+
test("URL", () => {
97+
expect(nicify(new URL("https://example.com"))).toBe('URL("https://example.com/")');
98+
});
99+
100+
test("TypedArrays", () => {
101+
expect(nicify(new Uint8Array([1, 2, 3]))).toBe("Uint8Array([1,2,3])");
102+
expect(nicify(new Int32Array([1, 2, 3]))).toBe("Int32Array([1,2,3])");
103+
});
104+
105+
test("Error objects", () => {
106+
const error = new Error("test error");
107+
const nicifiedError = nicify({ error });
108+
expect(nicifiedError).toMatch(new RegExp(deindent`
109+
^\{
110+
"error": Error: test error
111+
Stack:
112+
at (.|\\n)*
113+
\}$
114+
`));
115+
});
116+
117+
test("Error objects with cause and an extra property", () => {
118+
const error = new Error("test error", { cause: new Error("cause") });
119+
(error as any).extra = "something";
120+
const nicifiedError = nicify(error, { lineIndent: "--" });
121+
expect(nicifiedError).toMatch(new RegExp(deindent`
122+
^Error: test error
123+
--Stack:
124+
----at (.|\\n)+
125+
--Extra properties: \{ "extra": "something" \}
126+
--Cause:
127+
----Error: cause
128+
------Stack:
129+
--------at (.|\\n)+$
130+
`));
131+
});
132+
133+
test("Headers", () => {
134+
const headers = new Headers();
135+
headers.append("Content-Type", "application/json");
136+
headers.append("Accept", "text/plain");
137+
expect(nicify(headers)).toBe(deindent`
138+
Headers {
139+
"accept": "text/plain",
140+
"content-type": "application/json",
141+
}`
142+
);
143+
});
144+
});
145+
146+
describe("multiline strings", () => {
147+
test("basic multiline", () => {
148+
expect(nicify("line1\nline2")).toBe('deindent`\n line1\n line2\n`');
149+
});
150+
151+
test("multiline with trailing newline", () => {
152+
expect(nicify("line1\nline2\n")).toBe('deindent`\n line1\n line2\n` + "\\n"');
153+
});
154+
});
155+
156+
describe("circular references", () => {
157+
test("object with self reference", () => {
158+
const circular: any = { a: 1 };
159+
circular.self = circular;
160+
expect(nicify(circular)).toBe(deindent`
161+
{
162+
"a": 1,
163+
"self": Ref<value>,
164+
}`
165+
);
166+
});
167+
});
168+
169+
describe("configuration options", () => {
170+
test("maxDepth", () => {
171+
const deep = { a: { b: { c: { d: { e: 1 } } } } };
172+
expect(nicify(deep, { maxDepth: 2 })).toBe('{ "a": { "b": { ... } } }');
173+
});
174+
175+
test("lineIndent", () => {
176+
expect(nicify({ a: 1, b: 2 }, { lineIndent: " " })).toBe(deindent`
177+
{
178+
"a": 1,
179+
"b": 2,
180+
}
181+
`);
182+
});
183+
184+
test("hideFields", () => {
185+
expect(nicify({ a: 1, b: 2, secret: "hidden" }, { hideFields: ["secret"] })).toBe(deindent`
186+
{
187+
"a": 1,
188+
"b": 2,
189+
<some fields may have been hidden>,
190+
}
191+
`);
192+
});
193+
});
194+
195+
describe("custom overrides", () => {
196+
test("override with custom type", () => {
197+
expect(nicify({ type: "special" }, {
198+
overrides: ((value: unknown) => {
199+
if (typeof value === "object" && value && "type" in value && (value as any).type === "special") {
200+
return "SPECIAL";
201+
}
202+
return null;
203+
}) as NicifyOptions["overrides"]
204+
})).toBe("SPECIAL");
205+
});
206+
});
207+
208+
describe("functions", () => {
209+
test("named function", () => {
210+
expect(nicify(function namedFunction() {})).toBe("function namedFunction(...) { ... }");
211+
});
212+
213+
test("arrow function", () => {
214+
expect(nicify(() => {})).toBe("(...) => { ... }");
215+
});
216+
});
217+
218+
describe("Nicifiable interface", () => {
219+
test("object implementing Nicifiable", () => {
220+
const nicifiable = {
221+
value: 42,
222+
getNicifiableKeys() {
223+
return ["value"];
224+
},
225+
getNicifiedObjectExtraLines() {
226+
return ["// custom comment"];
227+
}
228+
};
229+
expect(nicify(nicifiable)).toBe(deindent`
230+
{
231+
"value": 42,
232+
// custom comment,
233+
}
234+
`);
235+
});
236+
});
237+
238+
describe("unknown types", () => {
239+
test("object without prototype", () => {
240+
const unknownType = Object.create(null);
241+
unknownType.value = "test";
242+
expect(nicify(unknownType)).toBe('{ "value": "test" }');
243+
});
244+
});
245+
});

packages/stack-shared/src/utils/strings.test.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)