Skip to content

Commit d95aa42

Browse files
committed
fix(serde): Error serialization
1 parent cdad46d commit d95aa42

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

Diff for: packages/qwik/src/core/shared/shared-serialization.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,8 @@ const inflate = (
310310
case TypeIds.Error: {
311311
const d = data as unknown[];
312312
target.message = d[0];
313-
const second = d[1];
314-
if (second && Array.isArray(second)) {
315-
for (let i = 0; i < second.length; i++) {
316-
target[second[i++]] = d[i];
317-
}
318-
target.stack = d[2];
319-
} else {
320-
target.stack = second;
313+
for (let i = 1; i < d.length; i += 2) {
314+
target[d[i] as string] = d[i + 1];
321315
}
322316
break;
323317
}
@@ -802,7 +796,7 @@ export const createSerializationContext = (
802796
) {
803797
// ignore
804798
} else if (obj instanceof Error) {
805-
discoveredValues.push(...Object.values(obj));
799+
discoveredValues.push(obj.message, ...Object.values(obj), isDev && obj.stack);
806800
} else if (isStore(obj)) {
807801
const target = getStoreTarget(obj)!;
808802
const effects = getStoreHandler(obj)!.$effects$;
@@ -1205,13 +1199,11 @@ function serialize(serializationContext: SerializationContext): void {
12051199
output(TypeIds.Regex, value.toString());
12061200
} else if (value instanceof Error) {
12071201
const out: any[] = [value.message];
1208-
const extraProps = Object.entries(value).flat();
1209-
if (extraProps.length) {
1210-
out.push(extraProps);
1211-
}
1202+
// flatten gives us the right output
1203+
out.push(...Object.entries(value).flat());
12121204
/// In production we don't want to leak the stack trace.
12131205
if (isDev) {
1214-
out.push(value.stack);
1206+
out.push('stack', value.stack);
12151207
}
12161208
output(TypeIds.Error, out);
12171209
} else if ($isSsrNode$(value)) {

Diff for: packages/qwik/src/core/shared/shared-serialization.unit.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ describe('shared-serialization', () => {
159159
"
160160
0 Error [
161161
String "hi"
162+
String "stack"
162163
String "Error: hi\\n at /...path/file.ts:123:456\\n at file:/...path/file.js:123:456\\n at file:/...path/file.js:123:456\\"...
163164
]
164165
(x chars)"
@@ -168,10 +169,9 @@ describe('shared-serialization', () => {
168169
"
169170
0 Error [
170171
String "hi"
171-
Array [
172-
String "extra"
173-
String "yey"
174-
]
172+
String "extra"
173+
String "yey"
174+
String "stack"
175175
String "Error: hi\\n at /...path/file.ts:123:456\\n at file:/...path/file.js:123:456\\n at file:/...path/file.js:123:456\\"...
176176
]
177177
(x chars)"

Diff for: packages/qwik/src/core/tests/container.spec.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,14 @@ describe('serializer v2', () => {
310310

311311
describe('ErrorSerializer, ///////// ' + TypeIds.Error, () => {
312312
it('should serialize and deserialize', async () => {
313-
const obj = Object.assign(new Error('MyError'), { extra: 'property' });
314-
expect((await withContainer((ssr) => ssr.addRoot(obj))).$getObjectById$(0)).toEqual(obj);
313+
const date = new Date();
314+
const obj = Object.assign(new Error('MyError'), {
315+
extra: { foo: ['bar', { hi: true }], bar: date },
316+
});
317+
const result = (await withContainer((ssr) => ssr.addRoot(obj))).$getObjectById$(0);
318+
expect(result.message).toEqual(obj.message);
319+
expect(result.extra.foo).toEqual(['bar', { hi: true }]);
320+
expect(result.extra.bar).toEqual(date);
315321
});
316322
});
317323

0 commit comments

Comments
 (0)