Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/rotten-spoons-reject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

Reject server function calls when the response is a 5xx without an X-Error header, instead of resolving with the parsed error body
10 changes: 10 additions & 0 deletions apps/tests/cypress/e2e/server-function.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ describe("server-function", () => {
cy.visit("/generator-server-function");
cy.get("#server-fn-test").contains('¡Hola, Mundo!');
});
it("should reject the promise when the response is a 5xx without an X-Error header", () => {
cy.intercept("POST", "/_server*", {
statusCode: 500,
headers: { "content-type": "application/json" },
body: { statusCode: 500, statusMessage: "boom" }
});
cy.visit("/server-function-rejected-on-500");
cy.get("#call").click();
cy.get("#server-fn-test").contains("rejected");
});
});
24 changes: 24 additions & 0 deletions apps/tests/src/routes/server-function-rejected-on-500.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createSignal } from "solid-js";
import { serverFnWithIsServer } from "~/functions/use-is-server";

export default function App() {
const [output, setOutput] = createSignal("pending");

const call = async () => {
try {
await serverFnWithIsServer();
setOutput("resolved");
} catch {
setOutput("rejected");
}
};

return (
<main>
<button id="call" onClick={() => void call()}>
call
</button>
<span id="server-fn-test">{output()}</span>
</main>
);
}
4 changes: 2 additions & 2 deletions packages/start/src/runtime/server-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ async function fetchServerFunction(
result = await deserializeJSONStream(cloned);
}
}
if (response.headers.has("X-Error")) {
throw result;
if (response.headers.has("X-Error") || response.status >= 500) {
throw result ?? new Error(`Server function call failed with status ${response.status}`);
}
return result;
}
Expand Down
Loading