@ivogt/rsc-router 0.0.0-experimental.1
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/README.md +19 -0
- package/package.json +131 -0
- package/src/__mocks__/version.ts +6 -0
- package/src/__tests__/route-definition.test.ts +63 -0
- package/src/browser/event-controller.ts +876 -0
- package/src/browser/index.ts +18 -0
- package/src/browser/link-interceptor.ts +121 -0
- package/src/browser/lru-cache.ts +69 -0
- package/src/browser/merge-segment-loaders.ts +126 -0
- package/src/browser/navigation-bridge.ts +891 -0
- package/src/browser/navigation-client.ts +155 -0
- package/src/browser/navigation-store.ts +823 -0
- package/src/browser/partial-update.ts +545 -0
- package/src/browser/react/Link.tsx +248 -0
- package/src/browser/react/NavigationProvider.tsx +228 -0
- package/src/browser/react/ScrollRestoration.tsx +94 -0
- package/src/browser/react/context.ts +53 -0
- package/src/browser/react/index.ts +52 -0
- package/src/browser/react/location-state-shared.ts +120 -0
- package/src/browser/react/location-state.ts +62 -0
- package/src/browser/react/use-action.ts +240 -0
- package/src/browser/react/use-client-cache.ts +56 -0
- package/src/browser/react/use-handle.ts +178 -0
- package/src/browser/react/use-link-status.ts +134 -0
- package/src/browser/react/use-navigation.ts +150 -0
- package/src/browser/react/use-segments.ts +188 -0
- package/src/browser/request-controller.ts +149 -0
- package/src/browser/rsc-router.tsx +310 -0
- package/src/browser/scroll-restoration.ts +324 -0
- package/src/browser/server-action-bridge.ts +747 -0
- package/src/browser/shallow.ts +35 -0
- package/src/browser/types.ts +443 -0
- package/src/cache/__tests__/memory-segment-store.test.ts +487 -0
- package/src/cache/__tests__/memory-store.test.ts +484 -0
- package/src/cache/cache-scope.ts +565 -0
- package/src/cache/cf/__tests__/cf-cache-store.test.ts +361 -0
- package/src/cache/cf/cf-cache-store.ts +274 -0
- package/src/cache/cf/index.ts +19 -0
- package/src/cache/index.ts +52 -0
- package/src/cache/memory-segment-store.ts +150 -0
- package/src/cache/memory-store.ts +253 -0
- package/src/cache/types.ts +366 -0
- package/src/client.rsc.tsx +88 -0
- package/src/client.tsx +609 -0
- package/src/components/DefaultDocument.tsx +20 -0
- package/src/default-error-boundary.tsx +88 -0
- package/src/deps/browser.ts +8 -0
- package/src/deps/html-stream-client.ts +2 -0
- package/src/deps/html-stream-server.ts +2 -0
- package/src/deps/rsc.ts +10 -0
- package/src/deps/ssr.ts +2 -0
- package/src/errors.ts +259 -0
- package/src/handle.ts +120 -0
- package/src/handles/MetaTags.tsx +178 -0
- package/src/handles/index.ts +6 -0
- package/src/handles/meta.ts +247 -0
- package/src/href-client.ts +128 -0
- package/src/href.ts +139 -0
- package/src/index.rsc.ts +69 -0
- package/src/index.ts +84 -0
- package/src/loader.rsc.ts +204 -0
- package/src/loader.ts +47 -0
- package/src/network-error-thrower.tsx +21 -0
- package/src/outlet-context.ts +15 -0
- package/src/root-error-boundary.tsx +277 -0
- package/src/route-content-wrapper.tsx +198 -0
- package/src/route-definition.ts +1333 -0
- package/src/route-map-builder.ts +140 -0
- package/src/route-types.ts +148 -0
- package/src/route-utils.ts +89 -0
- package/src/router/__tests__/match-context.test.ts +104 -0
- package/src/router/__tests__/match-pipelines.test.ts +537 -0
- package/src/router/__tests__/match-result.test.ts +566 -0
- package/src/router/__tests__/on-error.test.ts +935 -0
- package/src/router/__tests__/pattern-matching.test.ts +577 -0
- package/src/router/error-handling.ts +287 -0
- package/src/router/handler-context.ts +60 -0
- package/src/router/loader-resolution.ts +326 -0
- package/src/router/manifest.ts +116 -0
- package/src/router/match-context.ts +261 -0
- package/src/router/match-middleware/background-revalidation.ts +236 -0
- package/src/router/match-middleware/cache-lookup.ts +261 -0
- package/src/router/match-middleware/cache-store.ts +250 -0
- package/src/router/match-middleware/index.ts +81 -0
- package/src/router/match-middleware/intercept-resolution.ts +268 -0
- package/src/router/match-middleware/segment-resolution.ts +174 -0
- package/src/router/match-pipelines.ts +214 -0
- package/src/router/match-result.ts +212 -0
- package/src/router/metrics.ts +62 -0
- package/src/router/middleware.test.ts +1355 -0
- package/src/router/middleware.ts +748 -0
- package/src/router/pattern-matching.ts +271 -0
- package/src/router/revalidation.ts +190 -0
- package/src/router/router-context.ts +299 -0
- package/src/router/types.ts +96 -0
- package/src/router.ts +3484 -0
- package/src/rsc/__tests__/helpers.test.ts +175 -0
- package/src/rsc/handler.ts +942 -0
- package/src/rsc/helpers.ts +64 -0
- package/src/rsc/index.ts +56 -0
- package/src/rsc/nonce.ts +18 -0
- package/src/rsc/types.ts +225 -0
- package/src/segment-system.tsx +405 -0
- package/src/server/__tests__/request-context.test.ts +171 -0
- package/src/server/context.ts +340 -0
- package/src/server/handle-store.ts +230 -0
- package/src/server/loader-registry.ts +174 -0
- package/src/server/request-context.ts +470 -0
- package/src/server/root-layout.tsx +10 -0
- package/src/server/tsconfig.json +14 -0
- package/src/server.ts +126 -0
- package/src/ssr/__tests__/ssr-handler.test.tsx +188 -0
- package/src/ssr/index.tsx +215 -0
- package/src/types.ts +1473 -0
- package/src/use-loader.tsx +346 -0
- package/src/vite/__tests__/expose-loader-id.test.ts +117 -0
- package/src/vite/expose-action-id.ts +344 -0
- package/src/vite/expose-handle-id.ts +209 -0
- package/src/vite/expose-loader-id.ts +357 -0
- package/src/vite/expose-location-state-id.ts +177 -0
- package/src/vite/index.ts +608 -0
- package/src/vite/version.d.ts +12 -0
- package/src/vite/virtual-entries.ts +109 -0
|
@@ -0,0 +1,935 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import type { OnErrorCallback, OnErrorContext, ErrorPhase, ErrorInfo } from "../../types";
|
|
3
|
+
import { wrapLoaderWithErrorHandling, LoaderErrorCallback } from "../loader-resolution";
|
|
4
|
+
import { invokeOnError, type InvokeOnErrorContext } from "../error-handling";
|
|
5
|
+
|
|
6
|
+
describe("OnError Types", () => {
|
|
7
|
+
describe("OnErrorContext", () => {
|
|
8
|
+
it("should have all required properties", () => {
|
|
9
|
+
const context: OnErrorContext = {
|
|
10
|
+
error: new Error("Test error"),
|
|
11
|
+
phase: "routing",
|
|
12
|
+
request: new Request("https://example.com/test"),
|
|
13
|
+
url: new URL("https://example.com/test"),
|
|
14
|
+
pathname: "/test",
|
|
15
|
+
method: "GET",
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
expect(context.error).toBeInstanceOf(Error);
|
|
19
|
+
expect(context.phase).toBe("routing");
|
|
20
|
+
expect(context.request).toBeInstanceOf(Request);
|
|
21
|
+
expect(context.url).toBeInstanceOf(URL);
|
|
22
|
+
expect(context.pathname).toBe("/test");
|
|
23
|
+
expect(context.method).toBe("GET");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should support optional properties", () => {
|
|
27
|
+
const context: OnErrorContext = {
|
|
28
|
+
error: new Error("Test error"),
|
|
29
|
+
phase: "loader",
|
|
30
|
+
request: new Request("https://example.com/products/123"),
|
|
31
|
+
url: new URL("https://example.com/products/123"),
|
|
32
|
+
pathname: "/products/123",
|
|
33
|
+
method: "GET",
|
|
34
|
+
routeKey: "products.detail",
|
|
35
|
+
params: { id: "123" },
|
|
36
|
+
segmentId: "M1L0R0",
|
|
37
|
+
segmentType: "loader",
|
|
38
|
+
loaderName: "ProductLoader",
|
|
39
|
+
duration: 150.5,
|
|
40
|
+
isPartial: true,
|
|
41
|
+
handledByBoundary: true,
|
|
42
|
+
stack: "Error: Test error\n at ...",
|
|
43
|
+
metadata: { custom: "data" },
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
expect(context.routeKey).toBe("products.detail");
|
|
47
|
+
expect(context.params).toEqual({ id: "123" });
|
|
48
|
+
expect(context.segmentId).toBe("M1L0R0");
|
|
49
|
+
expect(context.segmentType).toBe("loader");
|
|
50
|
+
expect(context.loaderName).toBe("ProductLoader");
|
|
51
|
+
expect(context.duration).toBe(150.5);
|
|
52
|
+
expect(context.isPartial).toBe(true);
|
|
53
|
+
expect(context.handledByBoundary).toBe(true);
|
|
54
|
+
expect(context.stack).toBeDefined();
|
|
55
|
+
expect(context.metadata).toEqual({ custom: "data" });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should support action-specific properties", () => {
|
|
59
|
+
const context: OnErrorContext = {
|
|
60
|
+
error: new Error("Action failed"),
|
|
61
|
+
phase: "action",
|
|
62
|
+
request: new Request("https://example.com/api", { method: "POST" }),
|
|
63
|
+
url: new URL("https://example.com/api"),
|
|
64
|
+
pathname: "/api",
|
|
65
|
+
method: "POST",
|
|
66
|
+
actionId: "src/actions.ts#addToCart",
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
expect(context.phase).toBe("action");
|
|
70
|
+
expect(context.actionId).toBe("src/actions.ts#addToCart");
|
|
71
|
+
expect(context.method).toBe("POST");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should support middleware-specific properties", () => {
|
|
75
|
+
const context: OnErrorContext = {
|
|
76
|
+
error: new Error("Auth failed"),
|
|
77
|
+
phase: "middleware",
|
|
78
|
+
request: new Request("https://example.com/admin"),
|
|
79
|
+
url: new URL("https://example.com/admin"),
|
|
80
|
+
pathname: "/admin",
|
|
81
|
+
method: "GET",
|
|
82
|
+
middlewareId: "auth",
|
|
83
|
+
segmentType: "middleware",
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
expect(context.phase).toBe("middleware");
|
|
87
|
+
expect(context.middlewareId).toBe("auth");
|
|
88
|
+
expect(context.segmentType).toBe("middleware");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should support typed env", () => {
|
|
92
|
+
interface AppEnv {
|
|
93
|
+
DB: { query: () => void };
|
|
94
|
+
USER_ID: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const context: OnErrorContext<AppEnv> = {
|
|
98
|
+
error: new Error("DB error"),
|
|
99
|
+
phase: "loader",
|
|
100
|
+
request: new Request("https://example.com"),
|
|
101
|
+
url: new URL("https://example.com"),
|
|
102
|
+
pathname: "/",
|
|
103
|
+
method: "GET",
|
|
104
|
+
env: {
|
|
105
|
+
DB: { query: () => {} },
|
|
106
|
+
USER_ID: "user-123",
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
expect(context.env?.DB).toBeDefined();
|
|
111
|
+
expect(context.env?.USER_ID).toBe("user-123");
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("ErrorPhase", () => {
|
|
116
|
+
it("should include all valid phases", () => {
|
|
117
|
+
const phases: ErrorPhase[] = [
|
|
118
|
+
"routing",
|
|
119
|
+
"manifest",
|
|
120
|
+
"middleware",
|
|
121
|
+
"loader",
|
|
122
|
+
"handler",
|
|
123
|
+
"rendering",
|
|
124
|
+
"action",
|
|
125
|
+
"revalidation",
|
|
126
|
+
"unknown",
|
|
127
|
+
];
|
|
128
|
+
|
|
129
|
+
expect(phases).toHaveLength(9);
|
|
130
|
+
phases.forEach((phase) => {
|
|
131
|
+
expect(typeof phase).toBe("string");
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("OnErrorCallback", () => {
|
|
137
|
+
it("should accept sync callback", () => {
|
|
138
|
+
const errors: OnErrorContext[] = [];
|
|
139
|
+
const callback: OnErrorCallback = (context) => {
|
|
140
|
+
errors.push(context);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const context: OnErrorContext = {
|
|
144
|
+
error: new Error("Test"),
|
|
145
|
+
phase: "routing",
|
|
146
|
+
request: new Request("https://example.com"),
|
|
147
|
+
url: new URL("https://example.com"),
|
|
148
|
+
pathname: "/",
|
|
149
|
+
method: "GET",
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
callback(context);
|
|
153
|
+
expect(errors).toHaveLength(1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("should accept async callback", async () => {
|
|
157
|
+
const errors: OnErrorContext[] = [];
|
|
158
|
+
const callback: OnErrorCallback = async (context) => {
|
|
159
|
+
await Promise.resolve();
|
|
160
|
+
errors.push(context);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const context: OnErrorContext = {
|
|
164
|
+
error: new Error("Test"),
|
|
165
|
+
phase: "action",
|
|
166
|
+
request: new Request("https://example.com"),
|
|
167
|
+
url: new URL("https://example.com"),
|
|
168
|
+
pathname: "/",
|
|
169
|
+
method: "POST",
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
await callback(context);
|
|
173
|
+
expect(errors).toHaveLength(1);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("should work with typed env", () => {
|
|
177
|
+
interface CustomEnv {
|
|
178
|
+
secret: string;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const callback: OnErrorCallback<CustomEnv> = (context) => {
|
|
182
|
+
// Type-safe access to env
|
|
183
|
+
const secret = context.env?.secret;
|
|
184
|
+
expect(typeof secret).toBe("string");
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
callback({
|
|
188
|
+
error: new Error("Test"),
|
|
189
|
+
phase: "routing",
|
|
190
|
+
request: new Request("https://example.com"),
|
|
191
|
+
url: new URL("https://example.com"),
|
|
192
|
+
pathname: "/",
|
|
193
|
+
method: "GET",
|
|
194
|
+
env: { secret: "test-secret" },
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe("OnError Callback Integration", () => {
|
|
201
|
+
it("should capture error details from context", () => {
|
|
202
|
+
const capturedErrors: Array<{
|
|
203
|
+
message: string;
|
|
204
|
+
phase: ErrorPhase;
|
|
205
|
+
route?: string;
|
|
206
|
+
duration?: number;
|
|
207
|
+
}> = [];
|
|
208
|
+
|
|
209
|
+
const onError: OnErrorCallback = (context) => {
|
|
210
|
+
capturedErrors.push({
|
|
211
|
+
message: context.error.message,
|
|
212
|
+
phase: context.phase,
|
|
213
|
+
route: context.routeKey,
|
|
214
|
+
duration: context.duration,
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// Simulate errors from different phases
|
|
219
|
+
onError({
|
|
220
|
+
error: new Error("Route not found"),
|
|
221
|
+
phase: "routing",
|
|
222
|
+
request: new Request("https://example.com/unknown"),
|
|
223
|
+
url: new URL("https://example.com/unknown"),
|
|
224
|
+
pathname: "/unknown",
|
|
225
|
+
method: "GET",
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
onError({
|
|
229
|
+
error: new Error("Loader failed"),
|
|
230
|
+
phase: "loader",
|
|
231
|
+
request: new Request("https://example.com/products"),
|
|
232
|
+
url: new URL("https://example.com/products"),
|
|
233
|
+
pathname: "/products",
|
|
234
|
+
method: "GET",
|
|
235
|
+
routeKey: "products.list",
|
|
236
|
+
loaderName: "ProductsLoader",
|
|
237
|
+
duration: 100,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
onError({
|
|
241
|
+
error: new Error("Action failed"),
|
|
242
|
+
phase: "action",
|
|
243
|
+
request: new Request("https://example.com/cart", { method: "POST" }),
|
|
244
|
+
url: new URL("https://example.com/cart"),
|
|
245
|
+
pathname: "/cart",
|
|
246
|
+
method: "POST",
|
|
247
|
+
actionId: "addToCart",
|
|
248
|
+
duration: 50,
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
expect(capturedErrors).toHaveLength(3);
|
|
252
|
+
expect(capturedErrors[0]).toEqual({
|
|
253
|
+
message: "Route not found",
|
|
254
|
+
phase: "routing",
|
|
255
|
+
route: undefined,
|
|
256
|
+
duration: undefined,
|
|
257
|
+
});
|
|
258
|
+
expect(capturedErrors[1]).toEqual({
|
|
259
|
+
message: "Loader failed",
|
|
260
|
+
phase: "loader",
|
|
261
|
+
route: "products.list",
|
|
262
|
+
duration: 100,
|
|
263
|
+
});
|
|
264
|
+
expect(capturedErrors[2]).toEqual({
|
|
265
|
+
message: "Action failed",
|
|
266
|
+
phase: "action",
|
|
267
|
+
route: undefined,
|
|
268
|
+
duration: 50,
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("should handle errors thrown in callback gracefully", () => {
|
|
273
|
+
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
274
|
+
|
|
275
|
+
const badCallback: OnErrorCallback = () => {
|
|
276
|
+
throw new Error("Callback error");
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
// The callback throws, but it shouldn't propagate
|
|
280
|
+
// In real implementation, this is caught by invokeOnError
|
|
281
|
+
expect(() => {
|
|
282
|
+
try {
|
|
283
|
+
badCallback({
|
|
284
|
+
error: new Error("Original error"),
|
|
285
|
+
phase: "routing",
|
|
286
|
+
request: new Request("https://example.com"),
|
|
287
|
+
url: new URL("https://example.com"),
|
|
288
|
+
pathname: "/",
|
|
289
|
+
method: "GET",
|
|
290
|
+
});
|
|
291
|
+
} catch (e) {
|
|
292
|
+
// In real code, invokeOnError catches this
|
|
293
|
+
console.error("[Router.onError] Callback error:", e);
|
|
294
|
+
}
|
|
295
|
+
}).not.toThrow();
|
|
296
|
+
|
|
297
|
+
consoleErrorSpy.mockRestore();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("should handle async callback rejection gracefully", async () => {
|
|
301
|
+
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
302
|
+
|
|
303
|
+
const badAsyncCallback: OnErrorCallback = async () => {
|
|
304
|
+
await Promise.reject(new Error("Async callback error"));
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const context: OnErrorContext = {
|
|
308
|
+
error: new Error("Original error"),
|
|
309
|
+
phase: "action",
|
|
310
|
+
request: new Request("https://example.com"),
|
|
311
|
+
url: new URL("https://example.com"),
|
|
312
|
+
pathname: "/",
|
|
313
|
+
method: "POST",
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// In real implementation, the promise rejection is caught
|
|
317
|
+
const result = badAsyncCallback(context);
|
|
318
|
+
if (result instanceof Promise) {
|
|
319
|
+
await result.catch((e) => {
|
|
320
|
+
console.error("[Router.onError] Callback error:", e);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
consoleErrorSpy.mockRestore();
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
describe("wrapLoaderWithErrorHandling", () => {
|
|
329
|
+
const mockEntry = { id: "test-entry" } as any;
|
|
330
|
+
const mockPathname = "/test";
|
|
331
|
+
|
|
332
|
+
const createMockErrorInfo = (
|
|
333
|
+
error: unknown,
|
|
334
|
+
segmentId: string,
|
|
335
|
+
segmentType: ErrorInfo["segmentType"]
|
|
336
|
+
): ErrorInfo => ({
|
|
337
|
+
message: error instanceof Error ? error.message : String(error),
|
|
338
|
+
name: error instanceof Error ? error.name : "Error",
|
|
339
|
+
segmentId,
|
|
340
|
+
segmentType,
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
describe("successful resolution", () => {
|
|
344
|
+
it("should return ok: true with data on success", async () => {
|
|
345
|
+
const promise = Promise.resolve({ name: "Test Product" });
|
|
346
|
+
|
|
347
|
+
const result = await wrapLoaderWithErrorHandling(
|
|
348
|
+
promise,
|
|
349
|
+
mockEntry,
|
|
350
|
+
"M1L0.ProductLoader",
|
|
351
|
+
mockPathname,
|
|
352
|
+
() => null,
|
|
353
|
+
createMockErrorInfo
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
expect(result).toEqual({
|
|
357
|
+
__loaderResult: true,
|
|
358
|
+
ok: true,
|
|
359
|
+
data: { name: "Test Product" },
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("should not invoke onError on success", async () => {
|
|
364
|
+
const onError = vi.fn();
|
|
365
|
+
const promise = Promise.resolve("success");
|
|
366
|
+
|
|
367
|
+
await wrapLoaderWithErrorHandling(
|
|
368
|
+
promise,
|
|
369
|
+
mockEntry,
|
|
370
|
+
"M1L0.TestLoader",
|
|
371
|
+
mockPathname,
|
|
372
|
+
() => null,
|
|
373
|
+
createMockErrorInfo,
|
|
374
|
+
onError
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
expect(onError).not.toHaveBeenCalled();
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
describe("error handling without boundary", () => {
|
|
382
|
+
it("should return ok: false with error info when no boundary", async () => {
|
|
383
|
+
const promise = Promise.reject(new Error("Loader failed"));
|
|
384
|
+
|
|
385
|
+
const result = await wrapLoaderWithErrorHandling(
|
|
386
|
+
promise,
|
|
387
|
+
mockEntry,
|
|
388
|
+
"M1L0.FailingLoader",
|
|
389
|
+
mockPathname,
|
|
390
|
+
() => null, // No error boundary
|
|
391
|
+
createMockErrorInfo
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
expect(result).toEqual({
|
|
395
|
+
__loaderResult: true,
|
|
396
|
+
ok: false,
|
|
397
|
+
error: {
|
|
398
|
+
message: "Loader failed",
|
|
399
|
+
name: "Error",
|
|
400
|
+
segmentId: "M1L0.FailingLoader",
|
|
401
|
+
segmentType: "loader",
|
|
402
|
+
},
|
|
403
|
+
fallback: null,
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("should invoke onError with handledByBoundary: false", async () => {
|
|
408
|
+
const onError = vi.fn();
|
|
409
|
+
const testError = new Error("Test error");
|
|
410
|
+
const promise = Promise.reject(testError);
|
|
411
|
+
|
|
412
|
+
await wrapLoaderWithErrorHandling(
|
|
413
|
+
promise,
|
|
414
|
+
mockEntry,
|
|
415
|
+
"M1L0.TestLoader",
|
|
416
|
+
mockPathname,
|
|
417
|
+
() => null, // No boundary
|
|
418
|
+
createMockErrorInfo,
|
|
419
|
+
onError
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
expect(onError).toHaveBeenCalledWith(testError, {
|
|
423
|
+
segmentId: "M1L0.TestLoader",
|
|
424
|
+
loaderName: "TestLoader",
|
|
425
|
+
handledByBoundary: false,
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
describe("error handling with boundary", () => {
|
|
431
|
+
it("should return fallback when error boundary exists", async () => {
|
|
432
|
+
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
433
|
+
const promise = Promise.reject(new Error("Handled error"));
|
|
434
|
+
const fallbackElement = "Error Fallback UI";
|
|
435
|
+
|
|
436
|
+
const result = await wrapLoaderWithErrorHandling(
|
|
437
|
+
promise,
|
|
438
|
+
mockEntry,
|
|
439
|
+
"M1L0.HandledLoader",
|
|
440
|
+
mockPathname,
|
|
441
|
+
() => fallbackElement, // Has error boundary
|
|
442
|
+
createMockErrorInfo
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
expect(result).toEqual({
|
|
446
|
+
__loaderResult: true,
|
|
447
|
+
ok: false,
|
|
448
|
+
error: {
|
|
449
|
+
message: "Handled error",
|
|
450
|
+
name: "Error",
|
|
451
|
+
segmentId: "M1L0.HandledLoader",
|
|
452
|
+
segmentType: "loader",
|
|
453
|
+
},
|
|
454
|
+
fallback: fallbackElement,
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
consoleSpy.mockRestore();
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it("should invoke onError with handledByBoundary: true", async () => {
|
|
461
|
+
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
462
|
+
const onError = vi.fn();
|
|
463
|
+
const testError = new Error("Boundary handled");
|
|
464
|
+
const promise = Promise.reject(testError);
|
|
465
|
+
|
|
466
|
+
await wrapLoaderWithErrorHandling(
|
|
467
|
+
promise,
|
|
468
|
+
mockEntry,
|
|
469
|
+
"M1L0.BoundaryLoader",
|
|
470
|
+
mockPathname,
|
|
471
|
+
() => "Fallback", // Has boundary
|
|
472
|
+
createMockErrorInfo,
|
|
473
|
+
onError
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
expect(onError).toHaveBeenCalledWith(testError, {
|
|
477
|
+
segmentId: "M1L0.BoundaryLoader",
|
|
478
|
+
loaderName: "BoundaryLoader",
|
|
479
|
+
handledByBoundary: true,
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
consoleSpy.mockRestore();
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it("should call ErrorBoundaryHandler function with error props", async () => {
|
|
486
|
+
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
487
|
+
const promise = Promise.reject(new Error("Handler error"));
|
|
488
|
+
const boundaryHandler = vi.fn().mockReturnValue("Rendered Fallback");
|
|
489
|
+
|
|
490
|
+
const result = await wrapLoaderWithErrorHandling(
|
|
491
|
+
promise,
|
|
492
|
+
mockEntry,
|
|
493
|
+
"M1L0.HandlerLoader",
|
|
494
|
+
mockPathname,
|
|
495
|
+
() => boundaryHandler,
|
|
496
|
+
createMockErrorInfo
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
expect(boundaryHandler).toHaveBeenCalledWith({
|
|
500
|
+
error: {
|
|
501
|
+
message: "Handler error",
|
|
502
|
+
name: "Error",
|
|
503
|
+
segmentId: "M1L0.HandlerLoader",
|
|
504
|
+
segmentType: "loader",
|
|
505
|
+
},
|
|
506
|
+
});
|
|
507
|
+
if (!result.ok) {
|
|
508
|
+
expect(result.fallback).toBe("Rendered Fallback");
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
consoleSpy.mockRestore();
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
describe("loaderName extraction", () => {
|
|
516
|
+
it("should extract loader name from segmentId with dot notation", async () => {
|
|
517
|
+
const onError = vi.fn();
|
|
518
|
+
const promise = Promise.reject(new Error("test"));
|
|
519
|
+
|
|
520
|
+
await wrapLoaderWithErrorHandling(
|
|
521
|
+
promise,
|
|
522
|
+
mockEntry,
|
|
523
|
+
"M1L0D0.ProductLoader",
|
|
524
|
+
mockPathname,
|
|
525
|
+
() => null,
|
|
526
|
+
createMockErrorInfo,
|
|
527
|
+
onError
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
expect(onError).toHaveBeenCalledWith(
|
|
531
|
+
expect.any(Error),
|
|
532
|
+
expect.objectContaining({ loaderName: "ProductLoader" })
|
|
533
|
+
);
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
it("should handle segmentId without dots", async () => {
|
|
537
|
+
const onError = vi.fn();
|
|
538
|
+
const promise = Promise.reject(new Error("test"));
|
|
539
|
+
|
|
540
|
+
await wrapLoaderWithErrorHandling(
|
|
541
|
+
promise,
|
|
542
|
+
mockEntry,
|
|
543
|
+
"SimpleLoader",
|
|
544
|
+
mockPathname,
|
|
545
|
+
() => null,
|
|
546
|
+
createMockErrorInfo,
|
|
547
|
+
onError
|
|
548
|
+
);
|
|
549
|
+
|
|
550
|
+
expect(onError).toHaveBeenCalledWith(
|
|
551
|
+
expect.any(Error),
|
|
552
|
+
expect.objectContaining({ loaderName: "SimpleLoader" })
|
|
553
|
+
);
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
it("should return 'unknown' for empty segmentId", async () => {
|
|
557
|
+
const onError = vi.fn();
|
|
558
|
+
const promise = Promise.reject(new Error("test"));
|
|
559
|
+
|
|
560
|
+
await wrapLoaderWithErrorHandling(
|
|
561
|
+
promise,
|
|
562
|
+
mockEntry,
|
|
563
|
+
"",
|
|
564
|
+
mockPathname,
|
|
565
|
+
() => null,
|
|
566
|
+
createMockErrorInfo,
|
|
567
|
+
onError
|
|
568
|
+
);
|
|
569
|
+
|
|
570
|
+
expect(onError).toHaveBeenCalledWith(
|
|
571
|
+
expect.any(Error),
|
|
572
|
+
expect.objectContaining({ loaderName: "unknown" })
|
|
573
|
+
);
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
describe("non-Error objects", () => {
|
|
578
|
+
it("should handle string errors", async () => {
|
|
579
|
+
const promise = Promise.reject("String error message");
|
|
580
|
+
|
|
581
|
+
const result = await wrapLoaderWithErrorHandling(
|
|
582
|
+
promise,
|
|
583
|
+
mockEntry,
|
|
584
|
+
"M1L0.StringErrorLoader",
|
|
585
|
+
mockPathname,
|
|
586
|
+
() => null,
|
|
587
|
+
createMockErrorInfo
|
|
588
|
+
);
|
|
589
|
+
|
|
590
|
+
expect(result.ok).toBe(false);
|
|
591
|
+
if (!result.ok) {
|
|
592
|
+
expect(result.error?.message).toBe("String error message");
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
it("should handle object errors", async () => {
|
|
597
|
+
const promise = Promise.reject({ code: "ERR_001", detail: "Failed" });
|
|
598
|
+
|
|
599
|
+
const result = await wrapLoaderWithErrorHandling(
|
|
600
|
+
promise,
|
|
601
|
+
mockEntry,
|
|
602
|
+
"M1L0.ObjectErrorLoader",
|
|
603
|
+
mockPathname,
|
|
604
|
+
() => null,
|
|
605
|
+
(error) => ({
|
|
606
|
+
message: String(error),
|
|
607
|
+
name: "Error",
|
|
608
|
+
segmentId: "test",
|
|
609
|
+
segmentType: "loader" as const,
|
|
610
|
+
})
|
|
611
|
+
);
|
|
612
|
+
|
|
613
|
+
expect(result.ok).toBe(false);
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
it("should handle null/undefined errors", async () => {
|
|
617
|
+
const promise = Promise.reject(null);
|
|
618
|
+
|
|
619
|
+
const result = await wrapLoaderWithErrorHandling(
|
|
620
|
+
promise,
|
|
621
|
+
mockEntry,
|
|
622
|
+
"M1L0.NullErrorLoader",
|
|
623
|
+
mockPathname,
|
|
624
|
+
() => null,
|
|
625
|
+
(error) => ({
|
|
626
|
+
message: String(error),
|
|
627
|
+
name: "Error",
|
|
628
|
+
segmentId: "test",
|
|
629
|
+
segmentType: "loader" as const,
|
|
630
|
+
})
|
|
631
|
+
);
|
|
632
|
+
|
|
633
|
+
expect(result.ok).toBe(false);
|
|
634
|
+
if (!result.ok) {
|
|
635
|
+
expect(result.error?.message).toBe("null");
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
});
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
describe("Error Context Edge Cases", () => {
|
|
642
|
+
it("should handle errors with circular references in metadata", () => {
|
|
643
|
+
const circular: any = { name: "test" };
|
|
644
|
+
circular.self = circular;
|
|
645
|
+
|
|
646
|
+
const context: OnErrorContext = {
|
|
647
|
+
error: new Error("Circular test"),
|
|
648
|
+
phase: "loader",
|
|
649
|
+
request: new Request("https://example.com"),
|
|
650
|
+
url: new URL("https://example.com"),
|
|
651
|
+
pathname: "/",
|
|
652
|
+
method: "GET",
|
|
653
|
+
metadata: { circular }, // This shouldn't crash serialization
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
expect(context.metadata?.circular).toBe(circular);
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
it("should handle errors without stack traces", () => {
|
|
660
|
+
const errorWithoutStack = new Error("No stack");
|
|
661
|
+
delete errorWithoutStack.stack;
|
|
662
|
+
|
|
663
|
+
const context: OnErrorContext = {
|
|
664
|
+
error: errorWithoutStack,
|
|
665
|
+
phase: "routing",
|
|
666
|
+
request: new Request("https://example.com"),
|
|
667
|
+
url: new URL("https://example.com"),
|
|
668
|
+
pathname: "/",
|
|
669
|
+
method: "GET",
|
|
670
|
+
stack: errorWithoutStack.stack,
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
expect(context.stack).toBeUndefined();
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
it("should handle very long error messages", () => {
|
|
677
|
+
const longMessage = "x".repeat(10000);
|
|
678
|
+
const context: OnErrorContext = {
|
|
679
|
+
error: new Error(longMessage),
|
|
680
|
+
phase: "handler",
|
|
681
|
+
request: new Request("https://example.com"),
|
|
682
|
+
url: new URL("https://example.com"),
|
|
683
|
+
pathname: "/",
|
|
684
|
+
method: "GET",
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
expect(context.error.message.length).toBe(10000);
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
it("should preserve error cause chain", () => {
|
|
691
|
+
const rootCause = new Error("Root cause");
|
|
692
|
+
const wrappedError = new Error("Wrapped error", { cause: rootCause });
|
|
693
|
+
|
|
694
|
+
const context: OnErrorContext = {
|
|
695
|
+
error: wrappedError,
|
|
696
|
+
phase: "loader",
|
|
697
|
+
request: new Request("https://example.com"),
|
|
698
|
+
url: new URL("https://example.com"),
|
|
699
|
+
pathname: "/",
|
|
700
|
+
method: "GET",
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
expect(context.error.cause).toBe(rootCause);
|
|
704
|
+
});
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
describe("invokeOnError Shared Utility", () => {
|
|
708
|
+
const createMockContext = (): InvokeOnErrorContext => ({
|
|
709
|
+
request: new Request("https://example.com/test"),
|
|
710
|
+
url: new URL("https://example.com/test"),
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
describe("callback invocation", () => {
|
|
714
|
+
it("should not call callback if undefined", () => {
|
|
715
|
+
const context = createMockContext();
|
|
716
|
+
|
|
717
|
+
expect(() => {
|
|
718
|
+
invokeOnError(undefined, new Error("test"), "routing", context);
|
|
719
|
+
}).not.toThrow();
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
it("should invoke callback with full context", () => {
|
|
723
|
+
const callback = vi.fn();
|
|
724
|
+
const error = new Error("Test error");
|
|
725
|
+
const context: InvokeOnErrorContext = {
|
|
726
|
+
request: new Request("https://example.com/products/123"),
|
|
727
|
+
url: new URL("https://example.com/products/123"),
|
|
728
|
+
routeKey: "products.detail",
|
|
729
|
+
params: { id: "123" },
|
|
730
|
+
segmentId: "M1R0",
|
|
731
|
+
segmentType: "route",
|
|
732
|
+
env: { DB: "test" },
|
|
733
|
+
isPartial: true,
|
|
734
|
+
handledByBoundary: false,
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
invokeOnError(callback, error, "handler", context);
|
|
738
|
+
|
|
739
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
740
|
+
const receivedContext = callback.mock.calls[0][0];
|
|
741
|
+
expect(receivedContext.error).toBe(error);
|
|
742
|
+
expect(receivedContext.phase).toBe("handler");
|
|
743
|
+
expect(receivedContext.pathname).toBe("/products/123");
|
|
744
|
+
expect(receivedContext.method).toBe("GET");
|
|
745
|
+
expect(receivedContext.routeKey).toBe("products.detail");
|
|
746
|
+
expect(receivedContext.params).toEqual({ id: "123" });
|
|
747
|
+
expect(receivedContext.segmentId).toBe("M1R0");
|
|
748
|
+
expect(receivedContext.segmentType).toBe("route");
|
|
749
|
+
expect(receivedContext.env).toEqual({ DB: "test" });
|
|
750
|
+
expect(receivedContext.isPartial).toBe(true);
|
|
751
|
+
expect(receivedContext.handledByBoundary).toBe(false);
|
|
752
|
+
expect(receivedContext.stack).toBeDefined();
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
it("should convert non-Error objects to Error", () => {
|
|
756
|
+
const callback = vi.fn();
|
|
757
|
+
const context = createMockContext();
|
|
758
|
+
|
|
759
|
+
invokeOnError(callback, "string error", "routing", context);
|
|
760
|
+
|
|
761
|
+
const receivedContext = callback.mock.calls[0][0];
|
|
762
|
+
expect(receivedContext.error).toBeInstanceOf(Error);
|
|
763
|
+
expect(receivedContext.error.message).toBe("string error");
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it("should calculate duration from requestStartTime", () => {
|
|
767
|
+
const callback = vi.fn();
|
|
768
|
+
const context: InvokeOnErrorContext = {
|
|
769
|
+
...createMockContext(),
|
|
770
|
+
requestStartTime: performance.now() - 100, // 100ms ago
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
invokeOnError(callback, new Error("test"), "loader", context);
|
|
774
|
+
|
|
775
|
+
const receivedContext = callback.mock.calls[0][0];
|
|
776
|
+
expect(receivedContext.duration).toBeGreaterThanOrEqual(100);
|
|
777
|
+
expect(receivedContext.duration).toBeLessThan(200);
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
it("should not set duration if requestStartTime is not provided", () => {
|
|
781
|
+
const callback = vi.fn();
|
|
782
|
+
const context = createMockContext();
|
|
783
|
+
|
|
784
|
+
invokeOnError(callback, new Error("test"), "loader", context);
|
|
785
|
+
|
|
786
|
+
const receivedContext = callback.mock.calls[0][0];
|
|
787
|
+
expect(receivedContext.duration).toBeUndefined();
|
|
788
|
+
});
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
describe("error handling in callback", () => {
|
|
792
|
+
it("should catch sync callback errors and log them", () => {
|
|
793
|
+
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
794
|
+
const callback: OnErrorCallback = () => {
|
|
795
|
+
throw new Error("Callback sync error");
|
|
796
|
+
};
|
|
797
|
+
const context = createMockContext();
|
|
798
|
+
|
|
799
|
+
expect(() => {
|
|
800
|
+
invokeOnError(callback, new Error("original"), "routing", context);
|
|
801
|
+
}).not.toThrow();
|
|
802
|
+
|
|
803
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
804
|
+
"[Router.onError] Callback error:",
|
|
805
|
+
expect.any(Error)
|
|
806
|
+
);
|
|
807
|
+
consoleSpy.mockRestore();
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
it("should catch async callback rejections and log them", async () => {
|
|
811
|
+
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
812
|
+
const callback: OnErrorCallback = async () => {
|
|
813
|
+
throw new Error("Callback async error");
|
|
814
|
+
};
|
|
815
|
+
const context = createMockContext();
|
|
816
|
+
|
|
817
|
+
invokeOnError(callback, new Error("original"), "routing", context);
|
|
818
|
+
|
|
819
|
+
// Wait for the async rejection to be caught
|
|
820
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
821
|
+
|
|
822
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
823
|
+
"[Router.onError] Callback error:",
|
|
824
|
+
expect.any(Error)
|
|
825
|
+
);
|
|
826
|
+
consoleSpy.mockRestore();
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
it("should use custom log prefix", () => {
|
|
830
|
+
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
831
|
+
const callback: OnErrorCallback = () => {
|
|
832
|
+
throw new Error("Callback error");
|
|
833
|
+
};
|
|
834
|
+
const context = createMockContext();
|
|
835
|
+
|
|
836
|
+
invokeOnError(callback, new Error("original"), "rendering", context, "RSC");
|
|
837
|
+
|
|
838
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
839
|
+
"[RSC.onError] Callback error:",
|
|
840
|
+
expect.any(Error)
|
|
841
|
+
);
|
|
842
|
+
consoleSpy.mockRestore();
|
|
843
|
+
});
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
describe("all phases", () => {
|
|
847
|
+
const phases: ErrorPhase[] = [
|
|
848
|
+
"routing",
|
|
849
|
+
"manifest",
|
|
850
|
+
"middleware",
|
|
851
|
+
"loader",
|
|
852
|
+
"handler",
|
|
853
|
+
"rendering",
|
|
854
|
+
"action",
|
|
855
|
+
"revalidation",
|
|
856
|
+
"unknown",
|
|
857
|
+
];
|
|
858
|
+
|
|
859
|
+
phases.forEach((phase) => {
|
|
860
|
+
it(`should accept phase: ${phase}`, () => {
|
|
861
|
+
const callback = vi.fn();
|
|
862
|
+
const context = createMockContext();
|
|
863
|
+
|
|
864
|
+
invokeOnError(callback, new Error("test"), phase, context);
|
|
865
|
+
|
|
866
|
+
expect(callback).toHaveBeenCalledWith(
|
|
867
|
+
expect.objectContaining({ phase })
|
|
868
|
+
);
|
|
869
|
+
});
|
|
870
|
+
});
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
describe("optional context fields", () => {
|
|
874
|
+
it("should pass through all optional fields", () => {
|
|
875
|
+
const callback = vi.fn();
|
|
876
|
+
const context: InvokeOnErrorContext = {
|
|
877
|
+
request: new Request("https://example.com/api", { method: "POST" }),
|
|
878
|
+
url: new URL("https://example.com/api"),
|
|
879
|
+
routeKey: "api.action",
|
|
880
|
+
params: { id: "1" },
|
|
881
|
+
segmentId: "M1A0",
|
|
882
|
+
segmentType: "middleware",
|
|
883
|
+
loaderName: "TestLoader",
|
|
884
|
+
middlewareId: "auth",
|
|
885
|
+
actionId: "createItem",
|
|
886
|
+
env: { secret: "123" },
|
|
887
|
+
isPartial: false,
|
|
888
|
+
handledByBoundary: true,
|
|
889
|
+
metadata: { custom: "data" },
|
|
890
|
+
requestStartTime: performance.now() - 50,
|
|
891
|
+
};
|
|
892
|
+
|
|
893
|
+
invokeOnError(callback, new Error("test"), "action", context);
|
|
894
|
+
|
|
895
|
+
const received = callback.mock.calls[0][0];
|
|
896
|
+
expect(received.method).toBe("POST");
|
|
897
|
+
expect(received.loaderName).toBe("TestLoader");
|
|
898
|
+
expect(received.middlewareId).toBe("auth");
|
|
899
|
+
expect(received.actionId).toBe("createItem");
|
|
900
|
+
expect(received.metadata).toEqual({ custom: "data" });
|
|
901
|
+
});
|
|
902
|
+
|
|
903
|
+
it("should handle minimal context", () => {
|
|
904
|
+
const callback = vi.fn();
|
|
905
|
+
const context: InvokeOnErrorContext = {
|
|
906
|
+
request: new Request("https://example.com"),
|
|
907
|
+
url: new URL("https://example.com"),
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
invokeOnError(callback, new Error("test"), "unknown", context);
|
|
911
|
+
|
|
912
|
+
const received = callback.mock.calls[0][0];
|
|
913
|
+
expect(received.routeKey).toBeUndefined();
|
|
914
|
+
expect(received.params).toBeUndefined();
|
|
915
|
+
expect(received.segmentId).toBeUndefined();
|
|
916
|
+
expect(received.duration).toBeUndefined();
|
|
917
|
+
});
|
|
918
|
+
});
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
describe("LoaderErrorCallback Type", () => {
|
|
922
|
+
it("should accept valid callback signature", () => {
|
|
923
|
+
const callback: LoaderErrorCallback = (error, context) => {
|
|
924
|
+
expect(typeof context.segmentId).toBe("string");
|
|
925
|
+
expect(typeof context.loaderName).toBe("string");
|
|
926
|
+
expect(typeof context.handledByBoundary).toBe("boolean");
|
|
927
|
+
};
|
|
928
|
+
|
|
929
|
+
callback(new Error("test"), {
|
|
930
|
+
segmentId: "M1L0.TestLoader",
|
|
931
|
+
loaderName: "TestLoader",
|
|
932
|
+
handledByBoundary: false,
|
|
933
|
+
});
|
|
934
|
+
});
|
|
935
|
+
});
|