Skip to content

Commit

Permalink
Catch errors when trying to unwrap responses (#11061)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Dec 4, 2023
1 parent 7a5c0a7 commit dc7833c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/catch-unwrap-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Catch and bubble errors thrown when trying to unwrap responses from `loader`/`action` functions
69 changes: 69 additions & 0 deletions packages/router/__tests__/navigation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,75 @@ describe("navigations", () => {
});
});

it("handles errors when unwrapping Responses", async () => {
let t = setup({
routes: [
{
path: "/",
children: [
{
id: "foo",
path: "foo",
hasErrorBoundary: true,
loader: true,
},
],
},
],
});
let A = await t.navigate("/foo");
await A.loaders.foo.resolve(
// Invalid JSON
new Response('{"key":"value"}}}}}', {
status: 200,
headers: {
"Content-Type": "application/json",
},
})
);
expect(t.router.state.loaderData).toEqual({});
expect(t.router.state.errors).toMatchInlineSnapshot(`
{
"foo": [SyntaxError: Unexpected token } in JSON at position 15],
}
`);
});

it("bubbles errors when unwrapping Responses", async () => {
let t = setup({
routes: [
{
id: "root",
path: "/",
hasErrorBoundary: true,
children: [
{
id: "foo",
path: "foo",
loader: true,
},
],
},
],
});
let A = await t.navigate("/foo");
await A.loaders.foo.resolve(
// Invalid JSON
new Response('{"key":"value"}}}}}', {
status: 200,
headers: {
"Content-Type": "application/json",
},
})
);
expect(t.router.state.loaderData).toEqual({});
expect(t.router.state.errors).toMatchInlineSnapshot(`
{
"root": [SyntaxError: Unexpected token } in JSON at position 15],
}
`);
});

it("does not fetch unchanging layout data", async () => {
let t = initializeTest();
let A = await t.navigate("/foo");
Expand Down
19 changes: 12 additions & 7 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3990,13 +3990,18 @@ async function callLoaderOrAction(
}

let data: any;
let contentType = result.headers.get("Content-Type");
// Check between word boundaries instead of startsWith() due to the last
// paragraph of https://httpwg.org/specs/rfc9110.html#field.content-type
if (contentType && /\bapplication\/json\b/.test(contentType)) {
data = await result.json();
} else {
data = await result.text();

try {
let contentType = result.headers.get("Content-Type");
// Check between word boundaries instead of startsWith() due to the last
// paragraph of https://httpwg.org/specs/rfc9110.html#field.content-type
if (contentType && /\bapplication\/json\b/.test(contentType)) {
data = await result.json();
} else {
data = await result.text();
}
} catch (e) {
return { type: ResultType.error, error: e };
}

if (resultType === ResultType.error) {
Expand Down

0 comments on commit dc7833c

Please sign in to comment.