Skip to content

Commit 4bb6d22

Browse files
committed
fix(serde): Error serialization
1 parent 3ab0a99 commit 4bb6d22

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

packages/qwik/src/core/shared/shared-serialization.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,8 @@ const inflate = (
324324
case TypeIds.Error: {
325325
const d = data as unknown[];
326326
target.message = d[0];
327-
const second = d[1];
328-
if (second && Array.isArray(second)) {
329-
for (let i = 0; i < second.length; i++) {
330-
target[second[i++]] = second[i];
331-
}
332-
target.stack = d[2];
333-
} else {
334-
target.stack = second;
327+
for (let i = 1; i < d.length; i += 2) {
328+
target[d[i] as string] = d[i + 1];
335329
}
336330
break;
337331
}
@@ -818,7 +812,7 @@ export const createSerializationContext = (
818812
) {
819813
// ignore
820814
} else if (obj instanceof Error) {
821-
discoveredValues.push(...Object.values(obj));
815+
discoveredValues.push(obj.message, ...Object.values(obj), isDev && obj.stack);
822816
} else if (isStore(obj)) {
823817
const target = getStoreTarget(obj)!;
824818
const effects = getStoreHandler(obj)!.$effects$;
@@ -1268,13 +1262,11 @@ function serialize(serializationContext: SerializationContext): void {
12681262
output(TypeIds.Regex, value.toString());
12691263
} else if (value instanceof Error) {
12701264
const out: any[] = [value.message];
1271-
const extraProps = Object.entries(value).flat();
1272-
if (extraProps.length) {
1273-
out.push(extraProps);
1274-
}
1265+
// flatten gives us the right output
1266+
out.push(...Object.entries(value).flat());
12751267
/// In production we don't want to leak the stack trace.
12761268
if (isDev) {
1277-
out.push(value.stack);
1269+
out.push('stack', value.stack);
12781270
}
12791271
output(TypeIds.Error, out);
12801272
} else if ($isSsrNode$(value)) {

packages/qwik/src/core/shared/shared-serialization.unit.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ describe('shared-serialization', () => {
167167
"
168168
0 Error [
169169
String "hi"
170+
String "stack"
170171
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\\"...
171172
]
172173
(x chars)"
@@ -176,10 +177,9 @@ describe('shared-serialization', () => {
176177
"
177178
0 Error [
178179
String "hi"
179-
Array [
180-
String "extra"
181-
String "yey"
182-
]
180+
String "extra"
181+
String "yey"
182+
String "stack"
183183
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\\"...
184184
]
185185
(x chars)"

packages/qwik/src/core/tests/container.spec.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,14 @@ describe('serializer v2', () => {
311311

312312
describe('ErrorSerializer, ///////// ' + TypeIds.Error, () => {
313313
it('should serialize and deserialize', async () => {
314-
const obj = Object.assign(new Error('MyError'), { extra: 'property' });
314+
const date = new Date();
315+
const obj = Object.assign(new Error('MyError'), {
316+
extra: { foo: ['bar', { hi: true }], bar: date },
317+
});
315318
const result = (await withContainer((ssr) => ssr.addRoot(obj))).$getObjectById$(0);
316-
expect(result).toBeInstanceOf(Error);
317-
expect(result.message).toBe('MyError');
318-
expect((result as any).extra).toBe('property');
319+
expect(result.message).toEqual(obj.message);
320+
expect(result.extra.foo).toEqual(['bar', { hi: true }]);
321+
expect(result.extra.bar).toEqual(date);
319322
});
320323
});
321324

0 commit comments

Comments
 (0)