zero-query 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +2 -0
- package/cli/args.js +33 -33
- package/cli/commands/build-api.js +443 -442
- package/cli/commands/build.js +254 -247
- package/cli/commands/bundle.js +1228 -1224
- package/cli/commands/create.js +137 -121
- package/cli/commands/dev/devtools/index.js +56 -56
- package/cli/commands/dev/devtools/js/components.js +49 -49
- package/cli/commands/dev/devtools/js/core.js +423 -423
- package/cli/commands/dev/devtools/js/elements.js +421 -421
- package/cli/commands/dev/devtools/js/network.js +166 -166
- package/cli/commands/dev/devtools/js/performance.js +73 -73
- package/cli/commands/dev/devtools/js/router.js +105 -105
- package/cli/commands/dev/devtools/js/source.js +132 -132
- package/cli/commands/dev/devtools/js/stats.js +35 -35
- package/cli/commands/dev/devtools/js/tabs.js +79 -79
- package/cli/commands/dev/devtools/panel.html +95 -95
- package/cli/commands/dev/devtools/styles.css +244 -244
- package/cli/commands/dev/index.js +107 -107
- package/cli/commands/dev/logger.js +75 -75
- package/cli/commands/dev/overlay.js +858 -858
- package/cli/commands/dev/server.js +220 -220
- package/cli/commands/dev/validator.js +94 -94
- package/cli/commands/dev/watcher.js +172 -172
- package/cli/help.js +114 -112
- package/cli/index.js +52 -52
- package/cli/scaffold/default/LICENSE +21 -21
- package/cli/scaffold/default/app/app.js +207 -207
- package/cli/scaffold/default/app/components/about.js +201 -201
- package/cli/scaffold/default/app/components/api-demo.js +143 -143
- package/cli/scaffold/default/app/components/contact-card.js +231 -231
- package/cli/scaffold/default/app/components/contacts/contacts.css +706 -706
- package/cli/scaffold/default/app/components/contacts/contacts.html +200 -200
- package/cli/scaffold/default/app/components/contacts/contacts.js +196 -196
- package/cli/scaffold/default/app/components/counter.js +127 -127
- package/cli/scaffold/default/app/components/home.js +249 -249
- package/cli/scaffold/default/app/components/not-found.js +16 -16
- package/cli/scaffold/default/app/components/playground/playground.css +115 -115
- package/cli/scaffold/default/app/components/playground/playground.html +161 -161
- package/cli/scaffold/default/app/components/playground/playground.js +116 -116
- package/cli/scaffold/default/app/components/todos.js +225 -225
- package/cli/scaffold/default/app/components/toolkit/toolkit.css +97 -97
- package/cli/scaffold/default/app/components/toolkit/toolkit.html +146 -146
- package/cli/scaffold/default/app/components/toolkit/toolkit.js +280 -280
- package/cli/scaffold/default/app/routes.js +15 -15
- package/cli/scaffold/default/app/store.js +101 -101
- package/cli/scaffold/default/global.css +552 -552
- package/cli/scaffold/default/index.html +99 -99
- package/cli/scaffold/minimal/app/app.js +85 -85
- package/cli/scaffold/minimal/app/components/about.js +68 -68
- package/cli/scaffold/minimal/app/components/counter.js +122 -122
- package/cli/scaffold/minimal/app/components/home.js +68 -68
- package/cli/scaffold/minimal/app/components/not-found.js +16 -16
- package/cli/scaffold/minimal/app/routes.js +9 -9
- package/cli/scaffold/minimal/app/store.js +36 -36
- package/cli/scaffold/minimal/global.css +300 -300
- package/cli/scaffold/minimal/index.html +44 -44
- package/cli/scaffold/ssr/app/app.js +41 -41
- package/cli/scaffold/ssr/app/components/about.js +55 -55
- package/cli/scaffold/ssr/app/components/blog/index.js +65 -65
- package/cli/scaffold/ssr/app/components/blog/post.js +86 -86
- package/cli/scaffold/ssr/app/components/home.js +37 -37
- package/cli/scaffold/ssr/app/components/not-found.js +15 -15
- package/cli/scaffold/ssr/app/routes.js +8 -8
- package/cli/scaffold/ssr/global.css +228 -228
- package/cli/scaffold/ssr/index.html +37 -37
- package/cli/scaffold/ssr/package.json +8 -8
- package/cli/scaffold/ssr/server/data/posts.js +144 -144
- package/cli/scaffold/ssr/server/index.js +213 -213
- package/cli/scaffold/webrtc/app/app.js +11 -0
- package/cli/scaffold/webrtc/app/components/video-room.js +295 -0
- package/cli/scaffold/webrtc/app/lib/room.js +252 -0
- package/cli/scaffold/webrtc/assets/.gitkeep +0 -0
- package/cli/scaffold/webrtc/global.css +250 -0
- package/cli/scaffold/webrtc/index.html +21 -0
- package/cli/utils.js +305 -287
- package/dist/API.md +661 -0
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +10313 -6614
- package/dist/zquery.min.js +8 -631
- package/index.d.ts +570 -371
- package/index.js +311 -240
- package/package.json +76 -70
- package/src/component.js +1709 -1691
- package/src/core.js +921 -921
- package/src/diff.js +497 -497
- package/src/errors.js +209 -209
- package/src/expression.js +922 -922
- package/src/http.js +242 -242
- package/src/package.json +1 -1
- package/src/reactive.js +255 -255
- package/src/router.js +843 -843
- package/src/ssr.js +418 -418
- package/src/store.js +318 -318
- package/src/utils.js +515 -515
- package/src/webrtc/e2ee.js +351 -0
- package/src/webrtc/errors.js +116 -0
- package/src/webrtc/ice.js +301 -0
- package/src/webrtc/index.js +131 -0
- package/src/webrtc/joinToken.js +119 -0
- package/src/webrtc/observe.js +172 -0
- package/src/webrtc/peer.js +351 -0
- package/src/webrtc/reactive.js +268 -0
- package/src/webrtc/room.js +625 -0
- package/src/webrtc/sdp.js +302 -0
- package/src/webrtc/sfu/index.js +43 -0
- package/src/webrtc/sfu/livekit.js +131 -0
- package/src/webrtc/sfu/mediasoup.js +150 -0
- package/src/webrtc/signaling.js +373 -0
- package/src/webrtc/turn.js +237 -0
- package/tests/_helpers/webrtcFakes.js +289 -0
- package/tests/audit.test.js +4158 -4158
- package/tests/cli.test.js +1136 -1103
- package/tests/compare.test.js +497 -486
- package/tests/component.test.js +3969 -3938
- package/tests/core.test.js +1910 -1910
- package/tests/dev-server.test.js +489 -489
- package/tests/diff.test.js +1416 -1416
- package/tests/docs.test.js +1664 -1650
- package/tests/electron-features.test.js +864 -864
- package/tests/errors.test.js +619 -619
- package/tests/expression.test.js +1056 -1056
- package/tests/http.test.js +648 -648
- package/tests/reactive.test.js +819 -819
- package/tests/router.test.js +2327 -2327
- package/tests/ssr.test.js +870 -870
- package/tests/store.test.js +830 -830
- package/tests/test-minifier.js +153 -153
- package/tests/test-ssr.js +27 -27
- package/tests/utils.test.js +1377 -1377
- package/tests/webrtc/e2ee.test.js +283 -0
- package/tests/webrtc/ice.test.js +202 -0
- package/tests/webrtc/joinToken.test.js +89 -0
- package/tests/webrtc/observe.test.js +111 -0
- package/tests/webrtc/peer.test.js +373 -0
- package/tests/webrtc/reactive.test.js +235 -0
- package/tests/webrtc/room.test.js +406 -0
- package/tests/webrtc/sdp.test.js +151 -0
- package/tests/webrtc/sfu-livekit.test.js +119 -0
- package/tests/webrtc/sfu.test.js +160 -0
- package/tests/webrtc/signaling.test.js +251 -0
- package/tests/webrtc/turn.test.js +256 -0
- package/types/collection.d.ts +383 -383
- package/types/component.d.ts +186 -186
- package/types/errors.d.ts +135 -135
- package/types/http.d.ts +92 -92
- package/types/misc.d.ts +201 -201
- package/types/reactive.d.ts +98 -98
- package/types/router.d.ts +190 -190
- package/types/ssr.d.ts +102 -102
- package/types/store.d.ts +146 -146
- package/types/utils.d.ts +245 -245
- package/types/webrtc.d.ts +653 -0
package/tests/errors.test.js
CHANGED
|
@@ -1,619 +1,619 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
-
import { ZQueryError, ErrorCode, onError, reportError, guardCallback, guardAsync, validate, formatError } from '../src/errors.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// ---------------------------------------------------------------------------
|
|
6
|
-
// ZQueryError
|
|
7
|
-
// ---------------------------------------------------------------------------
|
|
8
|
-
|
|
9
|
-
describe('ZQueryError', () => {
|
|
10
|
-
it('extends Error', () => {
|
|
11
|
-
const err = new ZQueryError(ErrorCode.INVALID_ARGUMENT, 'test msg');
|
|
12
|
-
expect(err).toBeInstanceOf(Error);
|
|
13
|
-
expect(err).toBeInstanceOf(ZQueryError);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('sets code, message, and context', () => {
|
|
17
|
-
const err = new ZQueryError(ErrorCode.COMP_RENDER, 'render failed', { component: 'my-app' });
|
|
18
|
-
expect(err.code).toBe('ZQ_COMP_RENDER');
|
|
19
|
-
expect(err.message).toBe('render failed');
|
|
20
|
-
expect(err.context.component).toBe('my-app');
|
|
21
|
-
expect(err.name).toBe('ZQueryError');
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('stores cause', () => {
|
|
25
|
-
const cause = new Error('original');
|
|
26
|
-
const err = new ZQueryError(ErrorCode.HTTP_REQUEST, 'http error', {}, cause);
|
|
27
|
-
expect(err.cause).toBe(cause);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('has correct name', () => {
|
|
31
|
-
const err = new ZQueryError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
32
|
-
expect(err.name).toBe('ZQueryError');
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('has empty context by default', () => {
|
|
36
|
-
const err = new ZQueryError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
37
|
-
expect(err.context).toEqual({});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('has no cause property when not provided', () => {
|
|
41
|
-
const err = new ZQueryError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
42
|
-
expect(err.cause).toBeUndefined();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('has a stack trace', () => {
|
|
46
|
-
const err = new ZQueryError(ErrorCode.COMP_RENDER, 'test');
|
|
47
|
-
expect(err.stack).toBeDefined();
|
|
48
|
-
expect(typeof err.stack).toBe('string');
|
|
49
|
-
expect(err.stack.length).toBeGreaterThan(0);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('works with JSON.stringify (context serializable)', () => {
|
|
53
|
-
const err = new ZQueryError(ErrorCode.COMP_RENDER, 'test', { foo: 'bar' });
|
|
54
|
-
const json = JSON.stringify({ code: err.code, context: err.context });
|
|
55
|
-
expect(json).toContain('foo');
|
|
56
|
-
expect(json).toContain('bar');
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// ---------------------------------------------------------------------------
|
|
62
|
-
// ErrorCode
|
|
63
|
-
// ---------------------------------------------------------------------------
|
|
64
|
-
|
|
65
|
-
describe('ErrorCode', () => {
|
|
66
|
-
it('is frozen', () => {
|
|
67
|
-
expect(Object.isFrozen(ErrorCode)).toBe(true);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('contains expected codes', () => {
|
|
71
|
-
expect(ErrorCode.REACTIVE_CALLBACK).toBe('ZQ_REACTIVE_CALLBACK');
|
|
72
|
-
expect(ErrorCode.EXPR_PARSE).toBe('ZQ_EXPR_PARSE');
|
|
73
|
-
expect(ErrorCode.COMP_NOT_FOUND).toBe('ZQ_COMP_NOT_FOUND');
|
|
74
|
-
expect(ErrorCode.STORE_ACTION).toBe('ZQ_STORE_ACTION');
|
|
75
|
-
expect(ErrorCode.HTTP_REQUEST).toBe('ZQ_HTTP_REQUEST');
|
|
76
|
-
expect(ErrorCode.ROUTER_LOAD).toBe('ZQ_ROUTER_LOAD');
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('has reactive codes', () => {
|
|
80
|
-
expect(ErrorCode.REACTIVE_CALLBACK).toBe('ZQ_REACTIVE_CALLBACK');
|
|
81
|
-
expect(ErrorCode.SIGNAL_CALLBACK).toBe('ZQ_SIGNAL_CALLBACK');
|
|
82
|
-
expect(ErrorCode.EFFECT_EXEC).toBe('ZQ_EFFECT_EXEC');
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('has expression codes', () => {
|
|
86
|
-
expect(ErrorCode.EXPR_PARSE).toBe('ZQ_EXPR_PARSE');
|
|
87
|
-
expect(ErrorCode.EXPR_EVAL).toBe('ZQ_EXPR_EVAL');
|
|
88
|
-
expect(ErrorCode.EXPR_UNSAFE_ACCESS).toBe('ZQ_EXPR_UNSAFE_ACCESS');
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('has component codes', () => {
|
|
92
|
-
expect(ErrorCode.COMP_INVALID_NAME).toBe('ZQ_COMP_INVALID_NAME');
|
|
93
|
-
expect(ErrorCode.COMP_NOT_FOUND).toBe('ZQ_COMP_NOT_FOUND');
|
|
94
|
-
expect(ErrorCode.COMP_MOUNT_TARGET).toBe('ZQ_COMP_MOUNT_TARGET');
|
|
95
|
-
expect(ErrorCode.COMP_RENDER).toBe('ZQ_COMP_RENDER');
|
|
96
|
-
expect(ErrorCode.COMP_LIFECYCLE).toBe('ZQ_COMP_LIFECYCLE');
|
|
97
|
-
expect(ErrorCode.COMP_RESOURCE).toBe('ZQ_COMP_RESOURCE');
|
|
98
|
-
expect(ErrorCode.COMP_DIRECTIVE).toBe('ZQ_COMP_DIRECTIVE');
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('has router codes', () => {
|
|
102
|
-
expect(ErrorCode.ROUTER_LOAD).toBe('ZQ_ROUTER_LOAD');
|
|
103
|
-
expect(ErrorCode.ROUTER_GUARD).toBe('ZQ_ROUTER_GUARD');
|
|
104
|
-
expect(ErrorCode.ROUTER_RESOLVE).toBe('ZQ_ROUTER_RESOLVE');
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('has store codes', () => {
|
|
108
|
-
expect(ErrorCode.STORE_ACTION).toBe('ZQ_STORE_ACTION');
|
|
109
|
-
expect(ErrorCode.STORE_MIDDLEWARE).toBe('ZQ_STORE_MIDDLEWARE');
|
|
110
|
-
expect(ErrorCode.STORE_SUBSCRIBE).toBe('ZQ_STORE_SUBSCRIBE');
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('has http codes', () => {
|
|
114
|
-
expect(ErrorCode.HTTP_REQUEST).toBe('ZQ_HTTP_REQUEST');
|
|
115
|
-
expect(ErrorCode.HTTP_TIMEOUT).toBe('ZQ_HTTP_TIMEOUT');
|
|
116
|
-
expect(ErrorCode.HTTP_INTERCEPTOR).toBe('ZQ_HTTP_INTERCEPTOR');
|
|
117
|
-
expect(ErrorCode.HTTP_PARSE).toBe('ZQ_HTTP_PARSE');
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('has SSR codes', () => {
|
|
121
|
-
expect(ErrorCode.SSR_RENDER).toBe('ZQ_SSR_RENDER');
|
|
122
|
-
expect(ErrorCode.SSR_COMPONENT).toBe('ZQ_SSR_COMPONENT');
|
|
123
|
-
expect(ErrorCode.SSR_HYDRATION).toBe('ZQ_SSR_HYDRATION');
|
|
124
|
-
expect(ErrorCode.SSR_PAGE).toBe('ZQ_SSR_PAGE');
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('has general codes', () => {
|
|
128
|
-
expect(ErrorCode.INVALID_ARGUMENT).toBe('ZQ_INVALID_ARGUMENT');
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('all codes start with ZQ_ prefix', () => {
|
|
132
|
-
for (const [, value] of Object.entries(ErrorCode)) {
|
|
133
|
-
expect(value).toMatch(/^ZQ_/);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it('all codes are unique', () => {
|
|
138
|
-
const values = Object.values(ErrorCode);
|
|
139
|
-
const unique = new Set(values);
|
|
140
|
-
expect(unique.size).toBe(values.length);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
// ---------------------------------------------------------------------------
|
|
146
|
-
// onError / reportError - multi-handler support
|
|
147
|
-
// ---------------------------------------------------------------------------
|
|
148
|
-
|
|
149
|
-
describe('reportError', () => {
|
|
150
|
-
let errorSpy;
|
|
151
|
-
beforeEach(() => {
|
|
152
|
-
onError(null); // reset handlers
|
|
153
|
-
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
154
|
-
});
|
|
155
|
-
afterEach(() => {
|
|
156
|
-
errorSpy.mockRestore();
|
|
157
|
-
onError(null);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it('logs to console.error', () => {
|
|
161
|
-
reportError(ErrorCode.STORE_ACTION, 'test error', { action: 'foo' });
|
|
162
|
-
expect(errorSpy).toHaveBeenCalledOnce();
|
|
163
|
-
expect(errorSpy.mock.calls[0][0]).toContain('ZQ_STORE_ACTION');
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it('calls global error handler', () => {
|
|
167
|
-
const handler = vi.fn();
|
|
168
|
-
onError(handler);
|
|
169
|
-
reportError(ErrorCode.COMP_RENDER, 'render failed');
|
|
170
|
-
expect(handler).toHaveBeenCalledOnce();
|
|
171
|
-
const err = handler.mock.calls[0][0];
|
|
172
|
-
expect(err).toBeInstanceOf(ZQueryError);
|
|
173
|
-
expect(err.code).toBe('ZQ_COMP_RENDER');
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('does not crash if handler throws', () => {
|
|
177
|
-
onError(() => { throw new Error('handler error'); });
|
|
178
|
-
expect(() => reportError(ErrorCode.COMP_RENDER, 'test')).not.toThrow();
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it('passes cause through', () => {
|
|
182
|
-
const handler = vi.fn();
|
|
183
|
-
onError(handler);
|
|
184
|
-
const cause = new Error('root cause');
|
|
185
|
-
reportError(ErrorCode.HTTP_REQUEST, 'http failed', {}, cause);
|
|
186
|
-
expect(handler.mock.calls[0][0].cause).toBe(cause);
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it('supports multiple handlers', () => {
|
|
190
|
-
const handler1 = vi.fn();
|
|
191
|
-
const handler2 = vi.fn();
|
|
192
|
-
onError(handler1);
|
|
193
|
-
onError(handler2);
|
|
194
|
-
reportError(ErrorCode.COMP_RENDER, 'test');
|
|
195
|
-
expect(handler1).toHaveBeenCalledOnce();
|
|
196
|
-
expect(handler2).toHaveBeenCalledOnce();
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it('both handlers receive the same error', () => {
|
|
200
|
-
const errors = [];
|
|
201
|
-
onError(err => errors.push(err));
|
|
202
|
-
onError(err => errors.push(err));
|
|
203
|
-
reportError(ErrorCode.COMP_RENDER, 'shared');
|
|
204
|
-
expect(errors[0]).toBe(errors[1]);
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('handler can be removed via unsubscribe', () => {
|
|
208
|
-
const handler = vi.fn();
|
|
209
|
-
const unsub = onError(handler);
|
|
210
|
-
unsub();
|
|
211
|
-
reportError(ErrorCode.COMP_RENDER, 'test');
|
|
212
|
-
expect(handler).not.toHaveBeenCalled();
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
it('removing one handler does not affect others', () => {
|
|
216
|
-
const h1 = vi.fn();
|
|
217
|
-
const h2 = vi.fn();
|
|
218
|
-
const unsub1 = onError(h1);
|
|
219
|
-
onError(h2);
|
|
220
|
-
unsub1();
|
|
221
|
-
reportError(ErrorCode.COMP_RENDER, 'test');
|
|
222
|
-
expect(h1).not.toHaveBeenCalled();
|
|
223
|
-
expect(h2).toHaveBeenCalledOnce();
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
it('one handler throwing does not prevent others from running', () => {
|
|
227
|
-
const h1 = vi.fn(() => { throw new Error('boom'); });
|
|
228
|
-
const h2 = vi.fn();
|
|
229
|
-
onError(h1);
|
|
230
|
-
onError(h2);
|
|
231
|
-
reportError(ErrorCode.COMP_RENDER, 'test');
|
|
232
|
-
expect(h1).toHaveBeenCalledOnce();
|
|
233
|
-
expect(h2).toHaveBeenCalledOnce();
|
|
234
|
-
});
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
// ---------------------------------------------------------------------------
|
|
239
|
-
// onError - clearing and edge cases
|
|
240
|
-
// ---------------------------------------------------------------------------
|
|
241
|
-
|
|
242
|
-
describe('onError - edge cases', () => {
|
|
243
|
-
afterEach(() => onError(null));
|
|
244
|
-
|
|
245
|
-
it('passing null clears all handlers', () => {
|
|
246
|
-
const spy = vi.fn();
|
|
247
|
-
onError(spy);
|
|
248
|
-
onError(null);
|
|
249
|
-
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
250
|
-
reportError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
251
|
-
expect(spy).not.toHaveBeenCalled();
|
|
252
|
-
errorSpy.mockRestore();
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
it('passing non-function is ignored', () => {
|
|
256
|
-
const spy = vi.fn();
|
|
257
|
-
onError(spy);
|
|
258
|
-
onError('not a function');
|
|
259
|
-
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
260
|
-
reportError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
261
|
-
// Original handler should still be there
|
|
262
|
-
expect(spy).toHaveBeenCalled();
|
|
263
|
-
errorSpy.mockRestore();
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
it('handler exceptions do not crash reportError', () => {
|
|
267
|
-
onError(() => { throw new Error('handler crash'); });
|
|
268
|
-
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
269
|
-
expect(() => reportError(ErrorCode.INVALID_ARGUMENT, 'test')).not.toThrow();
|
|
270
|
-
errorSpy.mockRestore();
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it('onError returns an unsubscribe function', () => {
|
|
274
|
-
const unsub = onError(() => {});
|
|
275
|
-
expect(typeof unsub).toBe('function');
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
it('onError(null) returns a no-op unsubscribe', () => {
|
|
279
|
-
const unsub = onError(null);
|
|
280
|
-
expect(typeof unsub).toBe('function');
|
|
281
|
-
expect(() => unsub()).not.toThrow();
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
it('double unsubscribe is safe', () => {
|
|
285
|
-
const handler = vi.fn();
|
|
286
|
-
const unsub = onError(handler);
|
|
287
|
-
unsub();
|
|
288
|
-
expect(() => unsub()).not.toThrow();
|
|
289
|
-
});
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
// ---------------------------------------------------------------------------
|
|
294
|
-
// reportError - cause handling
|
|
295
|
-
// ---------------------------------------------------------------------------
|
|
296
|
-
|
|
297
|
-
describe('reportError - cause handling', () => {
|
|
298
|
-
let errorSpy;
|
|
299
|
-
beforeEach(() => {
|
|
300
|
-
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
301
|
-
});
|
|
302
|
-
afterEach(() => {
|
|
303
|
-
onError(null);
|
|
304
|
-
errorSpy.mockRestore();
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
it('reuses ZQueryError cause directly', () => {
|
|
308
|
-
const causes = [];
|
|
309
|
-
onError((err) => causes.push(err));
|
|
310
|
-
const existing = new ZQueryError(ErrorCode.COMP_RENDER, 'original');
|
|
311
|
-
reportError(ErrorCode.COMP_RENDER, 'wrapped', {}, existing);
|
|
312
|
-
expect(causes[0]).toBe(existing);
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
it('wraps non-ZQueryError cause', () => {
|
|
316
|
-
const causes = [];
|
|
317
|
-
onError((err) => causes.push(err));
|
|
318
|
-
const original = new Error('plain error');
|
|
319
|
-
reportError(ErrorCode.COMP_RENDER, 'wrapped', {}, original);
|
|
320
|
-
expect(causes[0]).toBeInstanceOf(ZQueryError);
|
|
321
|
-
expect(causes[0].cause).toBe(original);
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
it('reports error without cause', () => {
|
|
325
|
-
const handler = vi.fn();
|
|
326
|
-
onError(handler);
|
|
327
|
-
reportError(ErrorCode.STORE_ACTION, 'no cause');
|
|
328
|
-
expect(handler.mock.calls[0][0].cause).toBeUndefined();
|
|
329
|
-
});
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
// ---------------------------------------------------------------------------
|
|
334
|
-
// guardCallback
|
|
335
|
-
// ---------------------------------------------------------------------------
|
|
336
|
-
|
|
337
|
-
describe('guardCallback', () => {
|
|
338
|
-
let errorSpy;
|
|
339
|
-
beforeEach(() => {
|
|
340
|
-
onError(null);
|
|
341
|
-
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
342
|
-
});
|
|
343
|
-
afterEach(() => {
|
|
344
|
-
errorSpy.mockRestore();
|
|
345
|
-
onError(null);
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
it('returns the same value as original function', () => {
|
|
349
|
-
const guarded = guardCallback((x) => x * 2, ErrorCode.COMP_RENDER);
|
|
350
|
-
expect(guarded(5)).toBe(10);
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
it('catches errors and reports them', () => {
|
|
354
|
-
const handler = vi.fn();
|
|
355
|
-
onError(handler);
|
|
356
|
-
const guarded = guardCallback(() => { throw new Error('boom'); }, ErrorCode.COMP_RENDER, { component: 'test' });
|
|
357
|
-
expect(() => guarded()).not.toThrow();
|
|
358
|
-
expect(handler).toHaveBeenCalledOnce();
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
it('passes arguments through', () => {
|
|
362
|
-
const guarded = guardCallback((a, b) => a + b, ErrorCode.COMP_RENDER);
|
|
363
|
-
expect(guarded(1, 2)).toBe(3);
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
it('returns undefined on error', () => {
|
|
367
|
-
const guarded = guardCallback(() => { throw new Error('bang'); }, ErrorCode.INVALID_ARGUMENT);
|
|
368
|
-
expect(guarded()).toBeUndefined();
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
it('preserves context in error report', () => {
|
|
372
|
-
const handler = vi.fn();
|
|
373
|
-
onError(handler);
|
|
374
|
-
const guarded = guardCallback(
|
|
375
|
-
() => { throw new Error('bad'); },
|
|
376
|
-
ErrorCode.COMP_LIFECYCLE,
|
|
377
|
-
{ hook: 'mounted', component: 'my-app' }
|
|
378
|
-
);
|
|
379
|
-
guarded();
|
|
380
|
-
const err = handler.mock.calls[0][0];
|
|
381
|
-
expect(err.code).toBe(ErrorCode.COMP_LIFECYCLE);
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
it('works with zero arguments', () => {
|
|
385
|
-
const guarded = guardCallback(() => 'ok', ErrorCode.COMP_RENDER);
|
|
386
|
-
expect(guarded()).toBe('ok');
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
it('works with many arguments', () => {
|
|
390
|
-
const guarded = guardCallback((...args) => args.length, ErrorCode.COMP_RENDER);
|
|
391
|
-
expect(guarded(1, 2, 3, 4, 5)).toBe(5);
|
|
392
|
-
});
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
// ---------------------------------------------------------------------------
|
|
397
|
-
// guardAsync
|
|
398
|
-
// ---------------------------------------------------------------------------
|
|
399
|
-
|
|
400
|
-
describe('guardAsync', () => {
|
|
401
|
-
let errorSpy;
|
|
402
|
-
beforeEach(() => {
|
|
403
|
-
onError(null);
|
|
404
|
-
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
405
|
-
});
|
|
406
|
-
afterEach(() => {
|
|
407
|
-
errorSpy.mockRestore();
|
|
408
|
-
onError(null);
|
|
409
|
-
});
|
|
410
|
-
|
|
411
|
-
it('returns the resolved value on success', async () => {
|
|
412
|
-
const guarded = guardAsync(async (x) => x * 2, ErrorCode.COMP_RENDER);
|
|
413
|
-
expect(await guarded(5)).toBe(10);
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
it('catches async errors and reports them', async () => {
|
|
417
|
-
const handler = vi.fn();
|
|
418
|
-
onError(handler);
|
|
419
|
-
const guarded = guardAsync(async () => { throw new Error('async boom'); }, ErrorCode.HTTP_REQUEST);
|
|
420
|
-
const result = await guarded();
|
|
421
|
-
expect(result).toBeUndefined();
|
|
422
|
-
expect(handler).toHaveBeenCalledOnce();
|
|
423
|
-
expect(handler.mock.calls[0][0].code).toBe(ErrorCode.HTTP_REQUEST);
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
it('passes arguments through', async () => {
|
|
427
|
-
const guarded = guardAsync(async (a, b) => a + b, ErrorCode.COMP_RENDER);
|
|
428
|
-
expect(await guarded(3, 7)).toBe(10);
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
it('does not throw on rejection', async () => {
|
|
432
|
-
const guarded = guardAsync(async () => { throw new Error('fail'); }, ErrorCode.STORE_ACTION);
|
|
433
|
-
await expect(guarded()).resolves.toBeUndefined();
|
|
434
|
-
});
|
|
435
|
-
|
|
436
|
-
it('preserves context in error report', async () => {
|
|
437
|
-
const handler = vi.fn();
|
|
438
|
-
onError(handler);
|
|
439
|
-
const guarded = guardAsync(
|
|
440
|
-
async () => { throw new Error('bad'); },
|
|
441
|
-
ErrorCode.ROUTER_LOAD,
|
|
442
|
-
{ route: '/about' }
|
|
443
|
-
);
|
|
444
|
-
await guarded();
|
|
445
|
-
const err = handler.mock.calls[0][0];
|
|
446
|
-
expect(err.code).toBe(ErrorCode.ROUTER_LOAD);
|
|
447
|
-
});
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
// ---------------------------------------------------------------------------
|
|
452
|
-
// validate
|
|
453
|
-
// ---------------------------------------------------------------------------
|
|
454
|
-
|
|
455
|
-
describe('validate', () => {
|
|
456
|
-
it('passes for valid values', () => {
|
|
457
|
-
expect(() => validate('hello', 'name', 'string')).not.toThrow();
|
|
458
|
-
expect(() => validate(42, 'count', 'number')).not.toThrow();
|
|
459
|
-
expect(() => validate(() => {}, 'fn', 'function')).not.toThrow();
|
|
460
|
-
});
|
|
461
|
-
|
|
462
|
-
it('throws ZQueryError for null', () => {
|
|
463
|
-
expect(() => validate(null, 'name', 'string')).toThrow(ZQueryError);
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
it('throws ZQueryError for undefined', () => {
|
|
467
|
-
expect(() => validate(undefined, 'count')).toThrow(ZQueryError);
|
|
468
|
-
});
|
|
469
|
-
|
|
470
|
-
it('throws ZQueryError for wrong type', () => {
|
|
471
|
-
expect(() => validate(42, 'name', 'string')).toThrow(ZQueryError);
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
it('error has INVALID_ARGUMENT code', () => {
|
|
475
|
-
try {
|
|
476
|
-
validate(null, 'param');
|
|
477
|
-
} catch (err) {
|
|
478
|
-
expect(err.code).toBe(ErrorCode.INVALID_ARGUMENT);
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
|
|
482
|
-
it('validates without expectedType (null/undefined only)', () => {
|
|
483
|
-
expect(() => validate(null, 'x')).toThrow();
|
|
484
|
-
expect(() => validate(undefined, 'x')).toThrow();
|
|
485
|
-
expect(() => validate(0, 'x')).not.toThrow();
|
|
486
|
-
expect(() => validate('', 'x')).not.toThrow();
|
|
487
|
-
expect(() => validate(false, 'x')).not.toThrow();
|
|
488
|
-
});
|
|
489
|
-
|
|
490
|
-
it('error message contains parameter name', () => {
|
|
491
|
-
try {
|
|
492
|
-
validate(null, 'myParam');
|
|
493
|
-
expect.unreachable();
|
|
494
|
-
} catch (err) {
|
|
495
|
-
expect(err.message).toContain('myParam');
|
|
496
|
-
}
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
it('error message for wrong type contains expected type', () => {
|
|
500
|
-
try {
|
|
501
|
-
validate(42, 'name', 'string');
|
|
502
|
-
expect.unreachable();
|
|
503
|
-
} catch (err) {
|
|
504
|
-
expect(err.message).toContain('string');
|
|
505
|
-
expect(err.message).toContain('number');
|
|
506
|
-
}
|
|
507
|
-
});
|
|
508
|
-
|
|
509
|
-
it('accepts object type', () => {
|
|
510
|
-
expect(() => validate({}, 'cfg', 'object')).not.toThrow();
|
|
511
|
-
expect(() => validate('str', 'cfg', 'object')).toThrow();
|
|
512
|
-
});
|
|
513
|
-
|
|
514
|
-
it('accepts boolean type', () => {
|
|
515
|
-
expect(() => validate(true, 'flag', 'boolean')).not.toThrow();
|
|
516
|
-
expect(() => validate('yes', 'flag', 'boolean')).toThrow();
|
|
517
|
-
});
|
|
518
|
-
});
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
// ---------------------------------------------------------------------------
|
|
522
|
-
// formatError
|
|
523
|
-
// ---------------------------------------------------------------------------
|
|
524
|
-
|
|
525
|
-
describe('formatError', () => {
|
|
526
|
-
it('formats ZQueryError correctly', () => {
|
|
527
|
-
const err = new ZQueryError(ErrorCode.COMP_RENDER, 'render failed', { component: 'my-app' });
|
|
528
|
-
const formatted = formatError(err);
|
|
529
|
-
expect(formatted.code).toBe('ZQ_COMP_RENDER');
|
|
530
|
-
expect(formatted.type).toBe('ZQueryError');
|
|
531
|
-
expect(formatted.message).toBe('render failed');
|
|
532
|
-
expect(formatted.context.component).toBe('my-app');
|
|
533
|
-
expect(formatted.stack).toBeDefined();
|
|
534
|
-
expect(formatted.cause).toBeNull();
|
|
535
|
-
});
|
|
536
|
-
|
|
537
|
-
it('formats plain Error correctly', () => {
|
|
538
|
-
const err = new Error('plain error');
|
|
539
|
-
const formatted = formatError(err);
|
|
540
|
-
expect(formatted.code).toBe('');
|
|
541
|
-
expect(formatted.type).toBe('Error');
|
|
542
|
-
expect(formatted.message).toBe('plain error');
|
|
543
|
-
expect(formatted.context).toEqual({});
|
|
544
|
-
expect(formatted.cause).toBeNull();
|
|
545
|
-
});
|
|
546
|
-
|
|
547
|
-
it('formats nested cause chain', () => {
|
|
548
|
-
const root = new Error('root cause');
|
|
549
|
-
const err = new ZQueryError(ErrorCode.HTTP_REQUEST, 'http failed', {}, root);
|
|
550
|
-
const formatted = formatError(err);
|
|
551
|
-
expect(formatted.cause).not.toBeNull();
|
|
552
|
-
expect(formatted.cause.message).toBe('root cause');
|
|
553
|
-
expect(formatted.cause.type).toBe('Error');
|
|
554
|
-
});
|
|
555
|
-
|
|
556
|
-
it('handles TypeError correctly', () => {
|
|
557
|
-
const err = new TypeError('cannot read property');
|
|
558
|
-
const formatted = formatError(err);
|
|
559
|
-
expect(formatted.type).toBe('TypeError');
|
|
560
|
-
expect(formatted.code).toBe('');
|
|
561
|
-
});
|
|
562
|
-
|
|
563
|
-
it('handles error with no message', () => {
|
|
564
|
-
const err = new Error();
|
|
565
|
-
const formatted = formatError(err);
|
|
566
|
-
expect(formatted.message).toBeDefined();
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
it('returns serializable object', () => {
|
|
570
|
-
const err = new ZQueryError(ErrorCode.STORE_ACTION, 'test', { action: 'foo' });
|
|
571
|
-
const formatted = formatError(err);
|
|
572
|
-
const json = JSON.stringify(formatted);
|
|
573
|
-
expect(typeof json).toBe('string');
|
|
574
|
-
const parsed = JSON.parse(json);
|
|
575
|
-
expect(parsed.code).toBe('ZQ_STORE_ACTION');
|
|
576
|
-
});
|
|
577
|
-
});
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
// ---------------------------------------------------------------------------
|
|
581
|
-
// Integration: SSR errors use SSR_* codes
|
|
582
|
-
// ---------------------------------------------------------------------------
|
|
583
|
-
|
|
584
|
-
describe('SSR error integration', () => {
|
|
585
|
-
afterEach(() => onError(null));
|
|
586
|
-
|
|
587
|
-
it('SSR error codes exist and are properly prefixed', () => {
|
|
588
|
-
expect(ErrorCode.SSR_RENDER).toBe('ZQ_SSR_RENDER');
|
|
589
|
-
expect(ErrorCode.SSR_COMPONENT).toBe('ZQ_SSR_COMPONENT');
|
|
590
|
-
expect(ErrorCode.SSR_HYDRATION).toBe('ZQ_SSR_HYDRATION');
|
|
591
|
-
expect(ErrorCode.SSR_PAGE).toBe('ZQ_SSR_PAGE');
|
|
592
|
-
});
|
|
593
|
-
|
|
594
|
-
it('ZQueryError with SSR code works correctly', () => {
|
|
595
|
-
const err = new ZQueryError(ErrorCode.SSR_RENDER, 'SSR render failed', { component: 'my-page' });
|
|
596
|
-
expect(err.code).toBe('ZQ_SSR_RENDER');
|
|
597
|
-
expect(err.context.component).toBe('my-page');
|
|
598
|
-
expect(err).toBeInstanceOf(Error);
|
|
599
|
-
});
|
|
600
|
-
|
|
601
|
-
it('reportError with SSR codes works', () => {
|
|
602
|
-
const handler = vi.fn();
|
|
603
|
-
onError(handler);
|
|
604
|
-
const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
605
|
-
|
|
606
|
-
reportError(ErrorCode.SSR_RENDER, 'ssr test', { component: 'test' });
|
|
607
|
-
expect(handler).toHaveBeenCalledOnce();
|
|
608
|
-
expect(handler.mock.calls[0][0].code).toBe(ErrorCode.SSR_RENDER);
|
|
609
|
-
|
|
610
|
-
spy.mockRestore();
|
|
611
|
-
});
|
|
612
|
-
|
|
613
|
-
it('formatError works with SSR errors', () => {
|
|
614
|
-
const err = new ZQueryError(ErrorCode.SSR_COMPONENT, 'not registered', { component: 'foo' });
|
|
615
|
-
const formatted = formatError(err);
|
|
616
|
-
expect(formatted.code).toBe('ZQ_SSR_COMPONENT');
|
|
617
|
-
expect(formatted.context.component).toBe('foo');
|
|
618
|
-
});
|
|
619
|
-
});
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { ZQueryError, ErrorCode, onError, reportError, guardCallback, guardAsync, validate, formatError } from '../src/errors.js';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// ZQueryError
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
describe('ZQueryError', () => {
|
|
10
|
+
it('extends Error', () => {
|
|
11
|
+
const err = new ZQueryError(ErrorCode.INVALID_ARGUMENT, 'test msg');
|
|
12
|
+
expect(err).toBeInstanceOf(Error);
|
|
13
|
+
expect(err).toBeInstanceOf(ZQueryError);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('sets code, message, and context', () => {
|
|
17
|
+
const err = new ZQueryError(ErrorCode.COMP_RENDER, 'render failed', { component: 'my-app' });
|
|
18
|
+
expect(err.code).toBe('ZQ_COMP_RENDER');
|
|
19
|
+
expect(err.message).toBe('render failed');
|
|
20
|
+
expect(err.context.component).toBe('my-app');
|
|
21
|
+
expect(err.name).toBe('ZQueryError');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('stores cause', () => {
|
|
25
|
+
const cause = new Error('original');
|
|
26
|
+
const err = new ZQueryError(ErrorCode.HTTP_REQUEST, 'http error', {}, cause);
|
|
27
|
+
expect(err.cause).toBe(cause);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('has correct name', () => {
|
|
31
|
+
const err = new ZQueryError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
32
|
+
expect(err.name).toBe('ZQueryError');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('has empty context by default', () => {
|
|
36
|
+
const err = new ZQueryError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
37
|
+
expect(err.context).toEqual({});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('has no cause property when not provided', () => {
|
|
41
|
+
const err = new ZQueryError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
42
|
+
expect(err.cause).toBeUndefined();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('has a stack trace', () => {
|
|
46
|
+
const err = new ZQueryError(ErrorCode.COMP_RENDER, 'test');
|
|
47
|
+
expect(err.stack).toBeDefined();
|
|
48
|
+
expect(typeof err.stack).toBe('string');
|
|
49
|
+
expect(err.stack.length).toBeGreaterThan(0);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('works with JSON.stringify (context serializable)', () => {
|
|
53
|
+
const err = new ZQueryError(ErrorCode.COMP_RENDER, 'test', { foo: 'bar' });
|
|
54
|
+
const json = JSON.stringify({ code: err.code, context: err.context });
|
|
55
|
+
expect(json).toContain('foo');
|
|
56
|
+
expect(json).toContain('bar');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// ErrorCode
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
describe('ErrorCode', () => {
|
|
66
|
+
it('is frozen', () => {
|
|
67
|
+
expect(Object.isFrozen(ErrorCode)).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('contains expected codes', () => {
|
|
71
|
+
expect(ErrorCode.REACTIVE_CALLBACK).toBe('ZQ_REACTIVE_CALLBACK');
|
|
72
|
+
expect(ErrorCode.EXPR_PARSE).toBe('ZQ_EXPR_PARSE');
|
|
73
|
+
expect(ErrorCode.COMP_NOT_FOUND).toBe('ZQ_COMP_NOT_FOUND');
|
|
74
|
+
expect(ErrorCode.STORE_ACTION).toBe('ZQ_STORE_ACTION');
|
|
75
|
+
expect(ErrorCode.HTTP_REQUEST).toBe('ZQ_HTTP_REQUEST');
|
|
76
|
+
expect(ErrorCode.ROUTER_LOAD).toBe('ZQ_ROUTER_LOAD');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('has reactive codes', () => {
|
|
80
|
+
expect(ErrorCode.REACTIVE_CALLBACK).toBe('ZQ_REACTIVE_CALLBACK');
|
|
81
|
+
expect(ErrorCode.SIGNAL_CALLBACK).toBe('ZQ_SIGNAL_CALLBACK');
|
|
82
|
+
expect(ErrorCode.EFFECT_EXEC).toBe('ZQ_EFFECT_EXEC');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('has expression codes', () => {
|
|
86
|
+
expect(ErrorCode.EXPR_PARSE).toBe('ZQ_EXPR_PARSE');
|
|
87
|
+
expect(ErrorCode.EXPR_EVAL).toBe('ZQ_EXPR_EVAL');
|
|
88
|
+
expect(ErrorCode.EXPR_UNSAFE_ACCESS).toBe('ZQ_EXPR_UNSAFE_ACCESS');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('has component codes', () => {
|
|
92
|
+
expect(ErrorCode.COMP_INVALID_NAME).toBe('ZQ_COMP_INVALID_NAME');
|
|
93
|
+
expect(ErrorCode.COMP_NOT_FOUND).toBe('ZQ_COMP_NOT_FOUND');
|
|
94
|
+
expect(ErrorCode.COMP_MOUNT_TARGET).toBe('ZQ_COMP_MOUNT_TARGET');
|
|
95
|
+
expect(ErrorCode.COMP_RENDER).toBe('ZQ_COMP_RENDER');
|
|
96
|
+
expect(ErrorCode.COMP_LIFECYCLE).toBe('ZQ_COMP_LIFECYCLE');
|
|
97
|
+
expect(ErrorCode.COMP_RESOURCE).toBe('ZQ_COMP_RESOURCE');
|
|
98
|
+
expect(ErrorCode.COMP_DIRECTIVE).toBe('ZQ_COMP_DIRECTIVE');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('has router codes', () => {
|
|
102
|
+
expect(ErrorCode.ROUTER_LOAD).toBe('ZQ_ROUTER_LOAD');
|
|
103
|
+
expect(ErrorCode.ROUTER_GUARD).toBe('ZQ_ROUTER_GUARD');
|
|
104
|
+
expect(ErrorCode.ROUTER_RESOLVE).toBe('ZQ_ROUTER_RESOLVE');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('has store codes', () => {
|
|
108
|
+
expect(ErrorCode.STORE_ACTION).toBe('ZQ_STORE_ACTION');
|
|
109
|
+
expect(ErrorCode.STORE_MIDDLEWARE).toBe('ZQ_STORE_MIDDLEWARE');
|
|
110
|
+
expect(ErrorCode.STORE_SUBSCRIBE).toBe('ZQ_STORE_SUBSCRIBE');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('has http codes', () => {
|
|
114
|
+
expect(ErrorCode.HTTP_REQUEST).toBe('ZQ_HTTP_REQUEST');
|
|
115
|
+
expect(ErrorCode.HTTP_TIMEOUT).toBe('ZQ_HTTP_TIMEOUT');
|
|
116
|
+
expect(ErrorCode.HTTP_INTERCEPTOR).toBe('ZQ_HTTP_INTERCEPTOR');
|
|
117
|
+
expect(ErrorCode.HTTP_PARSE).toBe('ZQ_HTTP_PARSE');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('has SSR codes', () => {
|
|
121
|
+
expect(ErrorCode.SSR_RENDER).toBe('ZQ_SSR_RENDER');
|
|
122
|
+
expect(ErrorCode.SSR_COMPONENT).toBe('ZQ_SSR_COMPONENT');
|
|
123
|
+
expect(ErrorCode.SSR_HYDRATION).toBe('ZQ_SSR_HYDRATION');
|
|
124
|
+
expect(ErrorCode.SSR_PAGE).toBe('ZQ_SSR_PAGE');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('has general codes', () => {
|
|
128
|
+
expect(ErrorCode.INVALID_ARGUMENT).toBe('ZQ_INVALID_ARGUMENT');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('all codes start with ZQ_ prefix', () => {
|
|
132
|
+
for (const [, value] of Object.entries(ErrorCode)) {
|
|
133
|
+
expect(value).toMatch(/^ZQ_/);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('all codes are unique', () => {
|
|
138
|
+
const values = Object.values(ErrorCode);
|
|
139
|
+
const unique = new Set(values);
|
|
140
|
+
expect(unique.size).toBe(values.length);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
// onError / reportError - multi-handler support
|
|
147
|
+
// ---------------------------------------------------------------------------
|
|
148
|
+
|
|
149
|
+
describe('reportError', () => {
|
|
150
|
+
let errorSpy;
|
|
151
|
+
beforeEach(() => {
|
|
152
|
+
onError(null); // reset handlers
|
|
153
|
+
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
154
|
+
});
|
|
155
|
+
afterEach(() => {
|
|
156
|
+
errorSpy.mockRestore();
|
|
157
|
+
onError(null);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('logs to console.error', () => {
|
|
161
|
+
reportError(ErrorCode.STORE_ACTION, 'test error', { action: 'foo' });
|
|
162
|
+
expect(errorSpy).toHaveBeenCalledOnce();
|
|
163
|
+
expect(errorSpy.mock.calls[0][0]).toContain('ZQ_STORE_ACTION');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('calls global error handler', () => {
|
|
167
|
+
const handler = vi.fn();
|
|
168
|
+
onError(handler);
|
|
169
|
+
reportError(ErrorCode.COMP_RENDER, 'render failed');
|
|
170
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
171
|
+
const err = handler.mock.calls[0][0];
|
|
172
|
+
expect(err).toBeInstanceOf(ZQueryError);
|
|
173
|
+
expect(err.code).toBe('ZQ_COMP_RENDER');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('does not crash if handler throws', () => {
|
|
177
|
+
onError(() => { throw new Error('handler error'); });
|
|
178
|
+
expect(() => reportError(ErrorCode.COMP_RENDER, 'test')).not.toThrow();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('passes cause through', () => {
|
|
182
|
+
const handler = vi.fn();
|
|
183
|
+
onError(handler);
|
|
184
|
+
const cause = new Error('root cause');
|
|
185
|
+
reportError(ErrorCode.HTTP_REQUEST, 'http failed', {}, cause);
|
|
186
|
+
expect(handler.mock.calls[0][0].cause).toBe(cause);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('supports multiple handlers', () => {
|
|
190
|
+
const handler1 = vi.fn();
|
|
191
|
+
const handler2 = vi.fn();
|
|
192
|
+
onError(handler1);
|
|
193
|
+
onError(handler2);
|
|
194
|
+
reportError(ErrorCode.COMP_RENDER, 'test');
|
|
195
|
+
expect(handler1).toHaveBeenCalledOnce();
|
|
196
|
+
expect(handler2).toHaveBeenCalledOnce();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('both handlers receive the same error', () => {
|
|
200
|
+
const errors = [];
|
|
201
|
+
onError(err => errors.push(err));
|
|
202
|
+
onError(err => errors.push(err));
|
|
203
|
+
reportError(ErrorCode.COMP_RENDER, 'shared');
|
|
204
|
+
expect(errors[0]).toBe(errors[1]);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('handler can be removed via unsubscribe', () => {
|
|
208
|
+
const handler = vi.fn();
|
|
209
|
+
const unsub = onError(handler);
|
|
210
|
+
unsub();
|
|
211
|
+
reportError(ErrorCode.COMP_RENDER, 'test');
|
|
212
|
+
expect(handler).not.toHaveBeenCalled();
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('removing one handler does not affect others', () => {
|
|
216
|
+
const h1 = vi.fn();
|
|
217
|
+
const h2 = vi.fn();
|
|
218
|
+
const unsub1 = onError(h1);
|
|
219
|
+
onError(h2);
|
|
220
|
+
unsub1();
|
|
221
|
+
reportError(ErrorCode.COMP_RENDER, 'test');
|
|
222
|
+
expect(h1).not.toHaveBeenCalled();
|
|
223
|
+
expect(h2).toHaveBeenCalledOnce();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('one handler throwing does not prevent others from running', () => {
|
|
227
|
+
const h1 = vi.fn(() => { throw new Error('boom'); });
|
|
228
|
+
const h2 = vi.fn();
|
|
229
|
+
onError(h1);
|
|
230
|
+
onError(h2);
|
|
231
|
+
reportError(ErrorCode.COMP_RENDER, 'test');
|
|
232
|
+
expect(h1).toHaveBeenCalledOnce();
|
|
233
|
+
expect(h2).toHaveBeenCalledOnce();
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
// ---------------------------------------------------------------------------
|
|
239
|
+
// onError - clearing and edge cases
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
|
|
242
|
+
describe('onError - edge cases', () => {
|
|
243
|
+
afterEach(() => onError(null));
|
|
244
|
+
|
|
245
|
+
it('passing null clears all handlers', () => {
|
|
246
|
+
const spy = vi.fn();
|
|
247
|
+
onError(spy);
|
|
248
|
+
onError(null);
|
|
249
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
250
|
+
reportError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
251
|
+
expect(spy).not.toHaveBeenCalled();
|
|
252
|
+
errorSpy.mockRestore();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('passing non-function is ignored', () => {
|
|
256
|
+
const spy = vi.fn();
|
|
257
|
+
onError(spy);
|
|
258
|
+
onError('not a function');
|
|
259
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
260
|
+
reportError(ErrorCode.INVALID_ARGUMENT, 'test');
|
|
261
|
+
// Original handler should still be there
|
|
262
|
+
expect(spy).toHaveBeenCalled();
|
|
263
|
+
errorSpy.mockRestore();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('handler exceptions do not crash reportError', () => {
|
|
267
|
+
onError(() => { throw new Error('handler crash'); });
|
|
268
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
269
|
+
expect(() => reportError(ErrorCode.INVALID_ARGUMENT, 'test')).not.toThrow();
|
|
270
|
+
errorSpy.mockRestore();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('onError returns an unsubscribe function', () => {
|
|
274
|
+
const unsub = onError(() => {});
|
|
275
|
+
expect(typeof unsub).toBe('function');
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('onError(null) returns a no-op unsubscribe', () => {
|
|
279
|
+
const unsub = onError(null);
|
|
280
|
+
expect(typeof unsub).toBe('function');
|
|
281
|
+
expect(() => unsub()).not.toThrow();
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('double unsubscribe is safe', () => {
|
|
285
|
+
const handler = vi.fn();
|
|
286
|
+
const unsub = onError(handler);
|
|
287
|
+
unsub();
|
|
288
|
+
expect(() => unsub()).not.toThrow();
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
// ---------------------------------------------------------------------------
|
|
294
|
+
// reportError - cause handling
|
|
295
|
+
// ---------------------------------------------------------------------------
|
|
296
|
+
|
|
297
|
+
describe('reportError - cause handling', () => {
|
|
298
|
+
let errorSpy;
|
|
299
|
+
beforeEach(() => {
|
|
300
|
+
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
301
|
+
});
|
|
302
|
+
afterEach(() => {
|
|
303
|
+
onError(null);
|
|
304
|
+
errorSpy.mockRestore();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it('reuses ZQueryError cause directly', () => {
|
|
308
|
+
const causes = [];
|
|
309
|
+
onError((err) => causes.push(err));
|
|
310
|
+
const existing = new ZQueryError(ErrorCode.COMP_RENDER, 'original');
|
|
311
|
+
reportError(ErrorCode.COMP_RENDER, 'wrapped', {}, existing);
|
|
312
|
+
expect(causes[0]).toBe(existing);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('wraps non-ZQueryError cause', () => {
|
|
316
|
+
const causes = [];
|
|
317
|
+
onError((err) => causes.push(err));
|
|
318
|
+
const original = new Error('plain error');
|
|
319
|
+
reportError(ErrorCode.COMP_RENDER, 'wrapped', {}, original);
|
|
320
|
+
expect(causes[0]).toBeInstanceOf(ZQueryError);
|
|
321
|
+
expect(causes[0].cause).toBe(original);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('reports error without cause', () => {
|
|
325
|
+
const handler = vi.fn();
|
|
326
|
+
onError(handler);
|
|
327
|
+
reportError(ErrorCode.STORE_ACTION, 'no cause');
|
|
328
|
+
expect(handler.mock.calls[0][0].cause).toBeUndefined();
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
// ---------------------------------------------------------------------------
|
|
334
|
+
// guardCallback
|
|
335
|
+
// ---------------------------------------------------------------------------
|
|
336
|
+
|
|
337
|
+
describe('guardCallback', () => {
|
|
338
|
+
let errorSpy;
|
|
339
|
+
beforeEach(() => {
|
|
340
|
+
onError(null);
|
|
341
|
+
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
342
|
+
});
|
|
343
|
+
afterEach(() => {
|
|
344
|
+
errorSpy.mockRestore();
|
|
345
|
+
onError(null);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it('returns the same value as original function', () => {
|
|
349
|
+
const guarded = guardCallback((x) => x * 2, ErrorCode.COMP_RENDER);
|
|
350
|
+
expect(guarded(5)).toBe(10);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it('catches errors and reports them', () => {
|
|
354
|
+
const handler = vi.fn();
|
|
355
|
+
onError(handler);
|
|
356
|
+
const guarded = guardCallback(() => { throw new Error('boom'); }, ErrorCode.COMP_RENDER, { component: 'test' });
|
|
357
|
+
expect(() => guarded()).not.toThrow();
|
|
358
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it('passes arguments through', () => {
|
|
362
|
+
const guarded = guardCallback((a, b) => a + b, ErrorCode.COMP_RENDER);
|
|
363
|
+
expect(guarded(1, 2)).toBe(3);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it('returns undefined on error', () => {
|
|
367
|
+
const guarded = guardCallback(() => { throw new Error('bang'); }, ErrorCode.INVALID_ARGUMENT);
|
|
368
|
+
expect(guarded()).toBeUndefined();
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it('preserves context in error report', () => {
|
|
372
|
+
const handler = vi.fn();
|
|
373
|
+
onError(handler);
|
|
374
|
+
const guarded = guardCallback(
|
|
375
|
+
() => { throw new Error('bad'); },
|
|
376
|
+
ErrorCode.COMP_LIFECYCLE,
|
|
377
|
+
{ hook: 'mounted', component: 'my-app' }
|
|
378
|
+
);
|
|
379
|
+
guarded();
|
|
380
|
+
const err = handler.mock.calls[0][0];
|
|
381
|
+
expect(err.code).toBe(ErrorCode.COMP_LIFECYCLE);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it('works with zero arguments', () => {
|
|
385
|
+
const guarded = guardCallback(() => 'ok', ErrorCode.COMP_RENDER);
|
|
386
|
+
expect(guarded()).toBe('ok');
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it('works with many arguments', () => {
|
|
390
|
+
const guarded = guardCallback((...args) => args.length, ErrorCode.COMP_RENDER);
|
|
391
|
+
expect(guarded(1, 2, 3, 4, 5)).toBe(5);
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
// ---------------------------------------------------------------------------
|
|
397
|
+
// guardAsync
|
|
398
|
+
// ---------------------------------------------------------------------------
|
|
399
|
+
|
|
400
|
+
describe('guardAsync', () => {
|
|
401
|
+
let errorSpy;
|
|
402
|
+
beforeEach(() => {
|
|
403
|
+
onError(null);
|
|
404
|
+
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
405
|
+
});
|
|
406
|
+
afterEach(() => {
|
|
407
|
+
errorSpy.mockRestore();
|
|
408
|
+
onError(null);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it('returns the resolved value on success', async () => {
|
|
412
|
+
const guarded = guardAsync(async (x) => x * 2, ErrorCode.COMP_RENDER);
|
|
413
|
+
expect(await guarded(5)).toBe(10);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('catches async errors and reports them', async () => {
|
|
417
|
+
const handler = vi.fn();
|
|
418
|
+
onError(handler);
|
|
419
|
+
const guarded = guardAsync(async () => { throw new Error('async boom'); }, ErrorCode.HTTP_REQUEST);
|
|
420
|
+
const result = await guarded();
|
|
421
|
+
expect(result).toBeUndefined();
|
|
422
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
423
|
+
expect(handler.mock.calls[0][0].code).toBe(ErrorCode.HTTP_REQUEST);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it('passes arguments through', async () => {
|
|
427
|
+
const guarded = guardAsync(async (a, b) => a + b, ErrorCode.COMP_RENDER);
|
|
428
|
+
expect(await guarded(3, 7)).toBe(10);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it('does not throw on rejection', async () => {
|
|
432
|
+
const guarded = guardAsync(async () => { throw new Error('fail'); }, ErrorCode.STORE_ACTION);
|
|
433
|
+
await expect(guarded()).resolves.toBeUndefined();
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it('preserves context in error report', async () => {
|
|
437
|
+
const handler = vi.fn();
|
|
438
|
+
onError(handler);
|
|
439
|
+
const guarded = guardAsync(
|
|
440
|
+
async () => { throw new Error('bad'); },
|
|
441
|
+
ErrorCode.ROUTER_LOAD,
|
|
442
|
+
{ route: '/about' }
|
|
443
|
+
);
|
|
444
|
+
await guarded();
|
|
445
|
+
const err = handler.mock.calls[0][0];
|
|
446
|
+
expect(err.code).toBe(ErrorCode.ROUTER_LOAD);
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
// ---------------------------------------------------------------------------
|
|
452
|
+
// validate
|
|
453
|
+
// ---------------------------------------------------------------------------
|
|
454
|
+
|
|
455
|
+
describe('validate', () => {
|
|
456
|
+
it('passes for valid values', () => {
|
|
457
|
+
expect(() => validate('hello', 'name', 'string')).not.toThrow();
|
|
458
|
+
expect(() => validate(42, 'count', 'number')).not.toThrow();
|
|
459
|
+
expect(() => validate(() => {}, 'fn', 'function')).not.toThrow();
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it('throws ZQueryError for null', () => {
|
|
463
|
+
expect(() => validate(null, 'name', 'string')).toThrow(ZQueryError);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it('throws ZQueryError for undefined', () => {
|
|
467
|
+
expect(() => validate(undefined, 'count')).toThrow(ZQueryError);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('throws ZQueryError for wrong type', () => {
|
|
471
|
+
expect(() => validate(42, 'name', 'string')).toThrow(ZQueryError);
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
it('error has INVALID_ARGUMENT code', () => {
|
|
475
|
+
try {
|
|
476
|
+
validate(null, 'param');
|
|
477
|
+
} catch (err) {
|
|
478
|
+
expect(err.code).toBe(ErrorCode.INVALID_ARGUMENT);
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it('validates without expectedType (null/undefined only)', () => {
|
|
483
|
+
expect(() => validate(null, 'x')).toThrow();
|
|
484
|
+
expect(() => validate(undefined, 'x')).toThrow();
|
|
485
|
+
expect(() => validate(0, 'x')).not.toThrow();
|
|
486
|
+
expect(() => validate('', 'x')).not.toThrow();
|
|
487
|
+
expect(() => validate(false, 'x')).not.toThrow();
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
it('error message contains parameter name', () => {
|
|
491
|
+
try {
|
|
492
|
+
validate(null, 'myParam');
|
|
493
|
+
expect.unreachable();
|
|
494
|
+
} catch (err) {
|
|
495
|
+
expect(err.message).toContain('myParam');
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
it('error message for wrong type contains expected type', () => {
|
|
500
|
+
try {
|
|
501
|
+
validate(42, 'name', 'string');
|
|
502
|
+
expect.unreachable();
|
|
503
|
+
} catch (err) {
|
|
504
|
+
expect(err.message).toContain('string');
|
|
505
|
+
expect(err.message).toContain('number');
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it('accepts object type', () => {
|
|
510
|
+
expect(() => validate({}, 'cfg', 'object')).not.toThrow();
|
|
511
|
+
expect(() => validate('str', 'cfg', 'object')).toThrow();
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
it('accepts boolean type', () => {
|
|
515
|
+
expect(() => validate(true, 'flag', 'boolean')).not.toThrow();
|
|
516
|
+
expect(() => validate('yes', 'flag', 'boolean')).toThrow();
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
// ---------------------------------------------------------------------------
|
|
522
|
+
// formatError
|
|
523
|
+
// ---------------------------------------------------------------------------
|
|
524
|
+
|
|
525
|
+
describe('formatError', () => {
|
|
526
|
+
it('formats ZQueryError correctly', () => {
|
|
527
|
+
const err = new ZQueryError(ErrorCode.COMP_RENDER, 'render failed', { component: 'my-app' });
|
|
528
|
+
const formatted = formatError(err);
|
|
529
|
+
expect(formatted.code).toBe('ZQ_COMP_RENDER');
|
|
530
|
+
expect(formatted.type).toBe('ZQueryError');
|
|
531
|
+
expect(formatted.message).toBe('render failed');
|
|
532
|
+
expect(formatted.context.component).toBe('my-app');
|
|
533
|
+
expect(formatted.stack).toBeDefined();
|
|
534
|
+
expect(formatted.cause).toBeNull();
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
it('formats plain Error correctly', () => {
|
|
538
|
+
const err = new Error('plain error');
|
|
539
|
+
const formatted = formatError(err);
|
|
540
|
+
expect(formatted.code).toBe('');
|
|
541
|
+
expect(formatted.type).toBe('Error');
|
|
542
|
+
expect(formatted.message).toBe('plain error');
|
|
543
|
+
expect(formatted.context).toEqual({});
|
|
544
|
+
expect(formatted.cause).toBeNull();
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
it('formats nested cause chain', () => {
|
|
548
|
+
const root = new Error('root cause');
|
|
549
|
+
const err = new ZQueryError(ErrorCode.HTTP_REQUEST, 'http failed', {}, root);
|
|
550
|
+
const formatted = formatError(err);
|
|
551
|
+
expect(formatted.cause).not.toBeNull();
|
|
552
|
+
expect(formatted.cause.message).toBe('root cause');
|
|
553
|
+
expect(formatted.cause.type).toBe('Error');
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
it('handles TypeError correctly', () => {
|
|
557
|
+
const err = new TypeError('cannot read property');
|
|
558
|
+
const formatted = formatError(err);
|
|
559
|
+
expect(formatted.type).toBe('TypeError');
|
|
560
|
+
expect(formatted.code).toBe('');
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
it('handles error with no message', () => {
|
|
564
|
+
const err = new Error();
|
|
565
|
+
const formatted = formatError(err);
|
|
566
|
+
expect(formatted.message).toBeDefined();
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
it('returns serializable object', () => {
|
|
570
|
+
const err = new ZQueryError(ErrorCode.STORE_ACTION, 'test', { action: 'foo' });
|
|
571
|
+
const formatted = formatError(err);
|
|
572
|
+
const json = JSON.stringify(formatted);
|
|
573
|
+
expect(typeof json).toBe('string');
|
|
574
|
+
const parsed = JSON.parse(json);
|
|
575
|
+
expect(parsed.code).toBe('ZQ_STORE_ACTION');
|
|
576
|
+
});
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
// ---------------------------------------------------------------------------
|
|
581
|
+
// Integration: SSR errors use SSR_* codes
|
|
582
|
+
// ---------------------------------------------------------------------------
|
|
583
|
+
|
|
584
|
+
describe('SSR error integration', () => {
|
|
585
|
+
afterEach(() => onError(null));
|
|
586
|
+
|
|
587
|
+
it('SSR error codes exist and are properly prefixed', () => {
|
|
588
|
+
expect(ErrorCode.SSR_RENDER).toBe('ZQ_SSR_RENDER');
|
|
589
|
+
expect(ErrorCode.SSR_COMPONENT).toBe('ZQ_SSR_COMPONENT');
|
|
590
|
+
expect(ErrorCode.SSR_HYDRATION).toBe('ZQ_SSR_HYDRATION');
|
|
591
|
+
expect(ErrorCode.SSR_PAGE).toBe('ZQ_SSR_PAGE');
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
it('ZQueryError with SSR code works correctly', () => {
|
|
595
|
+
const err = new ZQueryError(ErrorCode.SSR_RENDER, 'SSR render failed', { component: 'my-page' });
|
|
596
|
+
expect(err.code).toBe('ZQ_SSR_RENDER');
|
|
597
|
+
expect(err.context.component).toBe('my-page');
|
|
598
|
+
expect(err).toBeInstanceOf(Error);
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
it('reportError with SSR codes works', () => {
|
|
602
|
+
const handler = vi.fn();
|
|
603
|
+
onError(handler);
|
|
604
|
+
const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
605
|
+
|
|
606
|
+
reportError(ErrorCode.SSR_RENDER, 'ssr test', { component: 'test' });
|
|
607
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
608
|
+
expect(handler.mock.calls[0][0].code).toBe(ErrorCode.SSR_RENDER);
|
|
609
|
+
|
|
610
|
+
spy.mockRestore();
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it('formatError works with SSR errors', () => {
|
|
614
|
+
const err = new ZQueryError(ErrorCode.SSR_COMPONENT, 'not registered', { component: 'foo' });
|
|
615
|
+
const formatted = formatError(err);
|
|
616
|
+
expect(formatted.code).toBe('ZQ_SSR_COMPONENT');
|
|
617
|
+
expect(formatted.context.component).toBe('foo');
|
|
618
|
+
});
|
|
619
|
+
});
|