Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch errors when trying to unwrap responses #11061

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -3991,13 +3991,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