Skip to content

Commit d849944

Browse files
committed
Report fetcher submission errors outward
1 parent 2b586c9 commit d849944

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/router/__tests__/router-test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9034,6 +9034,32 @@ describe("a router", () => {
90349034
});
90359035
});
90369036

9037+
it("action fetch with invalid body (json)", async () => {
9038+
let t = setup({
9039+
routes: [
9040+
{
9041+
id: "root",
9042+
path: "/",
9043+
hasErrorBoundary: true,
9044+
},
9045+
],
9046+
});
9047+
let A = await t.fetch("/", {
9048+
formMethod: "post",
9049+
body: "not json",
9050+
formEncType: "application/json",
9051+
});
9052+
expect(A.fetcher).toBe(IDLE_FETCHER);
9053+
expect(t.router.state.errors).toEqual({
9054+
index: new ErrorResponse(
9055+
400,
9056+
"Bad Request",
9057+
new Error("Unable to encode submission body"),
9058+
true
9059+
),
9060+
});
9061+
});
9062+
90379063
it("handles fetcher errors at contextual route boundaries", async () => {
90389064
let t = setup({
90399065
routes: [

packages/router/router.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1665,12 +1665,18 @@ export function createRouter(init: RouterInit): Router {
16651665
return;
16661666
}
16671667

1668-
let { path, submission } = normalizeNavigateOptions(
1668+
let { path, submission, error } = normalizeNavigateOptions(
16691669
future.v7_normalizeFormMethod,
16701670
true,
16711671
normalizedPath,
16721672
opts
16731673
);
1674+
1675+
if (error) {
1676+
setFetcherError(key, routeId, error);
1677+
return;
1678+
}
1679+
16741680
let match = getTargetMatch(matches, path);
16751681

16761682
pendingPreventScrollReset = (opts && opts.preventScrollReset) === true;

0 commit comments

Comments
 (0)