Skip to content

Commit 9de7d42

Browse files
committed
Add test + changeset
1 parent 267c2c7 commit 9de7d42

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

.changeset/six-ways-carry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Preserve `statusText` on the `ErrorResponse` instance when throwing `data()` from a route handler

packages/react-router/__tests__/router/router-test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
AgnosticDataRouteObject,
66
AgnosticRouteObject,
77
} from "../../lib/router/utils";
8-
import { ErrorResponseImpl } from "../../lib/router/utils";
8+
import { data, ErrorResponseImpl } from "../../lib/router/utils";
99

1010
import { urlMatch } from "./utils/custom-matchers";
1111
import {
@@ -1915,6 +1915,33 @@ describe("a router", () => {
19151915
});
19161916
});
19171917

1918+
it("handles thrown data() values as ErrorResponse's", async () => {
1919+
let t = setup({
1920+
routes: TASK_ROUTES,
1921+
initialEntries: ["/"],
1922+
hydrationData: {
1923+
loaderData: {
1924+
root: "ROOT_DATA",
1925+
index: "INDEX_DATA",
1926+
},
1927+
},
1928+
});
1929+
1930+
let nav = await t.navigate("/tasks");
1931+
await nav.loaders.tasks.reject(
1932+
data("broken", { status: 400, statusText: "Bad Request" }),
1933+
);
1934+
expect(t.router.state).toMatchObject({
1935+
navigation: IDLE_NAVIGATION,
1936+
loaderData: {
1937+
root: "ROOT_DATA",
1938+
},
1939+
errors: {
1940+
tasks: new ErrorResponseImpl(400, "Bad Request", "broken"),
1941+
},
1942+
});
1943+
});
1944+
19181945
it("sends proper arguments to actions", async () => {
19191946
let t = setup({
19201947
routes: TASK_ROUTES,

packages/react-router/lib/router/router.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6212,11 +6212,7 @@ async function convertDataStrategyResultToDataResult(
62126212
// Convert thrown data() to ErrorResponse instances
62136213
return {
62146214
type: ResultType.error,
6215-
error: new ErrorResponseImpl(
6216-
result.init?.status || 500,
6217-
result.init?.statusText,
6218-
result.data,
6219-
),
6215+
error: dataWithResponseInitToErrorResponse(result),
62206216
statusCode: isRouteErrorResponse(result) ? result.status : undefined,
62216217
headers: result.init?.headers
62226218
? new Headers(result.init.headers)

0 commit comments

Comments
 (0)