Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tighten RSC error reporting tests
  • Loading branch information
brookslybrand committed Oct 20, 2026
commit 2a146b45c57d699e8351eee020fe5d0b5ce918f5
54 changes: 0 additions & 54 deletions integration/rsc-server-error-logging-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,11 @@ test.describe("RSC Framework: server error logging", () => {
return <h1>LOADER_ERROR_ROUTE</h1>;
}
`,
"app/routes/action-error.tsx": js`
export function action() {
throw new Error("ACTION_ERROR_MESSAGE");
}

export default function ActionError() {
return <h1>ACTION_ERROR_ROUTE</h1>;
}
`,
"app/routes/resource-error.tsx": js`
export function loader() {
throw new Error("RESOURCE_ERROR_MESSAGE");
}
`,
"app/server-functions.ts": js`
"use server";

export async function failingServerFunction() {
throw new Error("SERVER_FUNCTION_ERROR_MESSAGE");
}
`,
"app/routes/server-function-error.tsx": js`
import { failingServerFunction } from "../server-functions";

export function ServerComponent() {
return (
<form action={failingServerFunction}>
<button type="submit">Submit</button>
</form>
);
}
`,
},
});
});
Expand Down Expand Up @@ -117,37 +90,10 @@ test.describe("RSC Framework: server error logging", () => {
expectLogged("LOADER_ERROR_MESSAGE");
});

test("logs errors thrown from actions", async () => {
let response = await fixture.postDocument(
"/action-error",
new URLSearchParams({ intent: "fail" }),
);
expect(response.status).toBe(500);
expect(await response.text()).toContain("ROOT_ERROR_BOUNDARY");
expectLogged("ACTION_ERROR_MESSAGE");
});

test("logs errors thrown from resource routes", async () => {
let response = await fixture.requestResource("/resource-error");
expect(response.status).toBe(500);
await response.text();
expectLogged("RESOURCE_ERROR_MESSAGE");
});

test("logs errors thrown from server functions", async () => {
let html = await (
await fixture.requestDocument("/server-function-error")
).text();
let actionField = html.match(/name="(\$ACTION_ID_[^"]+)"/)?.[1];
expect(actionField).toBeTruthy();

let formData = new FormData();
formData.append(actionField!, "");
let response = await fixture.requestDocument("/server-function-error", {
method: "POST",
body: formData,
});
await response.text();
expectLogged("SERVER_FUNCTION_ERROR_MESSAGE");
});
});
109 changes: 45 additions & 64 deletions packages/react-router/__tests__/rsc/server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,18 @@ describe("RSC server", () => {

describe("error reporting", () => {
let consoleError: jest.SpyInstance;
let abortController: AbortController;

beforeEach(() => {
consoleError = jest.spyOn(console, "error").mockImplementation(() => {});
abortController = new AbortController();
});

afterEach(() => {
consoleError.mockRestore();
});

let errorRoutes: RSCRouteConfigEntry[] = [
let routes: RSCRouteConfigEntry[] = [
{
id: "root",
path: "/",
Expand All @@ -431,14 +433,6 @@ describe("RSC server", () => {
throw new Error("ACTION_ERROR");
},
},
{
id: "thrown-response",
path: "thrown-response",
Component: () => null,
loader() {
throw new Response("Nope", { status: 401 });
},
},
{
id: "middleware-error",
path: "middleware-error",
Expand All @@ -449,73 +443,64 @@ describe("RSC server", () => {
},
],
},
{
id: "thrown-response",
path: "thrown-response",
Component: () => null,
loader() {
throw new Response("Nope", { status: 401 });
},
},
{
id: "aborted-resource",
path: "aborted-resource",
loader() {
abortController.abort();
throw new Error("ABORTED_ERROR");
},
},
],
},
];

function matchErrorRequest(
request: Request,
onError?: (error: unknown) => void,
) {
function match(request: Request, onError?: (error: unknown) => void) {
return matchRSCServerRequest({
createTemporaryReferenceSet: () => ({}),
request,
routes: errorRoutes,
routes,
onError,
generateResponse(match) {
return new Response(null, { status: match.statusCode });
},
});
}

test("calls onError with loader errors", async () => {
let onError = jest.fn();
let response = await matchErrorRequest(
new Request("https://remix.run/loader-error"),
onError,
);
expect(response.status).toBe(500);
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0]).toEqual(new Error("LOADER_ERROR"));
});

test("calls onError with loader errors on data requests", async () => {
let onError = jest.fn();
await matchErrorRequest(
new Request("https://remix.run/loader-error.rsc"),
onError,
);
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0]).toEqual(new Error("LOADER_ERROR"));
});

test("calls onError with action errors", async () => {
let onError = jest.fn();
let response = await matchErrorRequest(
test.each([
["loader", new Request("https://remix.run/loader-error"), "LOADER_ERROR"],
[
"action",
new Request("https://remix.run/action-error", {
method: "POST",
body: new URLSearchParams({ a: "b" }),
}),
onError,
);
expect(response.status).toBe(500);
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0]).toEqual(new Error("ACTION_ERROR"));
});

test("calls onError with middleware errors", async () => {
let onError = jest.fn();
await matchErrorRequest(
"ACTION_ERROR",
],
[
"middleware",
new Request("https://remix.run/middleware-error"),
onError,
);
"MIDDLEWARE_ERROR",
],
])("calls onError with %s errors", async (_, request, message) => {
let onError = jest.fn();
let response = await match(request, onError);
expect(response.status).toBe(500);
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0]).toEqual(new Error("MIDDLEWARE_ERROR"));
expect(onError).toHaveBeenCalledWith(new Error(message));
});

test("does not call onError with thrown responses", async () => {
let onError = jest.fn();
let response = await matchErrorRequest(
let response = await match(
new Request("https://remix.run/thrown-response"),
onError,
);
Expand All @@ -524,29 +509,25 @@ describe("RSC server", () => {
});

test("logs errors with console.error when no onError is provided", async () => {
await matchErrorRequest(new Request("https://remix.run/loader-error"));
await match(new Request("https://remix.run/loader-error"));
expect(consoleError).toHaveBeenCalledTimes(1);
expect(consoleError.mock.calls[0][0]).toEqual(new Error("LOADER_ERROR"));
expect(consoleError).toHaveBeenCalledWith(new Error("LOADER_ERROR"));
});

test("logs the underlying error for internal error responses", async () => {
await matchErrorRequest(new Request("https://remix.run/does-not-exist"));
await match(new Request("https://remix.run/does-not-exist"));
expect(consoleError).toHaveBeenCalledTimes(1);
expect(consoleError.mock.calls[0][0]).toBeInstanceOf(Error);
expect(consoleError.mock.calls[0][0].message).toMatch(
'No route matches URL "/does-not-exist"',
expect(consoleError).toHaveBeenCalledWith(
new Error('No route matches URL "/does-not-exist"'),
);
});

test("does not log errors for aborted requests", async () => {
let controller = new AbortController();
let response = matchErrorRequest(
new Request("https://remix.run/loader-error", {
signal: controller.signal,
await match(
new Request("https://remix.run/aborted-resource", {
signal: abortController.signal,
}),
);
controller.abort();
await response.catch(() => {});
expect(consoleError).not.toHaveBeenCalled();
});
});
Expand Down
Loading