Skip to content

Commit add6f8a

Browse files
authored
Respect the ErrorResponse status code if passed to getStaticContextFormError (#11213)
1 parent c9f8a7b commit add6f8a

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Respect the `ErrorResponse` status code if passed to `getStaticContextFormError`

packages/router/__tests__/ssr-test.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
*/
44

55
import type { StaticHandler, StaticHandlerContext } from "../router";
6-
import { UNSAFE_DEFERRED_SYMBOL, createStaticHandler } from "../router";
6+
import {
7+
UNSAFE_DEFERRED_SYMBOL,
8+
createStaticHandler,
9+
getStaticContextFromError,
10+
} from "../router";
711
import {
812
ErrorResponseImpl,
913
defer,
@@ -13,7 +17,7 @@ import {
1317
} from "../utils";
1418
import { deferredData, trackedPromise } from "./utils/custom-matchers";
1519
import { createDeferred } from "./utils/data-router-setup";
16-
import { createRequest, createSubmitRequest } from "./utils/utils";
20+
import { createRequest, createSubmitRequest, invariant } from "./utils/utils";
1721

1822
interface CustomMatchers<R = jest.Expect> {
1923
trackedPromise(data?: any, error?: any, aborted?: boolean): R;
@@ -1385,6 +1389,58 @@ describe("ssr", () => {
13851389
]);
13861390
});
13871391
});
1392+
1393+
describe("getStaticContextFromError", () => {
1394+
it("should provide a context for a second-pass render for a thrown error", async () => {
1395+
let { query } = createStaticHandler(SSR_ROUTES);
1396+
let context = await query(createRequest("/"));
1397+
expect(context).toMatchObject({
1398+
errors: null,
1399+
loaderData: {
1400+
index: "INDEX LOADER",
1401+
},
1402+
statusCode: 200,
1403+
});
1404+
1405+
let error = new Error("💥");
1406+
invariant(!(context instanceof Response), "Uh oh");
1407+
context = getStaticContextFromError(SSR_ROUTES, context, error);
1408+
expect(context).toMatchObject({
1409+
errors: {
1410+
index: error,
1411+
},
1412+
loaderData: {
1413+
index: "INDEX LOADER",
1414+
},
1415+
statusCode: 500,
1416+
});
1417+
});
1418+
1419+
it("should accept a thrown response from entry.server", async () => {
1420+
let { query } = createStaticHandler(SSR_ROUTES);
1421+
let context = await query(createRequest("/"));
1422+
expect(context).toMatchObject({
1423+
errors: null,
1424+
loaderData: {
1425+
index: "INDEX LOADER",
1426+
},
1427+
statusCode: 200,
1428+
});
1429+
1430+
let errorResponse = new ErrorResponseImpl(400, "Bad Request", "Oops!");
1431+
invariant(!(context instanceof Response), "Uh oh");
1432+
context = getStaticContextFromError(SSR_ROUTES, context, errorResponse);
1433+
expect(context).toMatchObject({
1434+
errors: {
1435+
index: errorResponse,
1436+
},
1437+
loaderData: {
1438+
index: "INDEX LOADER",
1439+
},
1440+
statusCode: 400,
1441+
});
1442+
});
1443+
});
13881444
});
13891445

13901446
describe("singular route requests", () => {

packages/router/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3368,7 +3368,7 @@ export function getStaticContextFromError(
33683368
) {
33693369
let newContext: StaticHandlerContext = {
33703370
...context,
3371-
statusCode: 500,
3371+
statusCode: isRouteErrorResponse(error) ? error.status : 500,
33723372
errors: {
33733373
[context._deepestRenderedBoundaryId || routes[0].id]: error,
33743374
},

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,9 +3644,9 @@ camelcase@^6.0.0, camelcase@^6.2.0:
36443644
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==
36453645

36463646
caniuse-lite@^1.0.30001517:
3647-
version "1.0.30001518"
3648-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001518.tgz#b3ca93904cb4699c01218246c4d77a71dbe97150"
3649-
integrity sha512-rup09/e3I0BKjncL+FesTayKtPrdwKhUufQFd3riFw1hHg8JmIFoInYfB102cFcY/pPgGmdyl/iy+jgiDi2vdA==
3647+
version "1.0.30001579"
3648+
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001579.tgz"
3649+
integrity sha512-u5AUVkixruKHJjw/pj9wISlcMpgFWzSrczLZbrqBSxukQixmg0SJ5sZTpvaFvxU0HoQKd4yoyAogyrAz9pzJnA==
36503650

36513651
ccount@^2.0.0:
36523652
version "2.0.1"

0 commit comments

Comments
 (0)