Skip to content

Commit d62f09c

Browse files
adipascubrenelz
andauthored
fix: reject server function calls on 5xx responses without X-Error (#2159)
Co-authored-by: Brenley Dueck <brenleydueck@gmail.com>
1 parent fe9b18f commit d62f09c

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

.changeset/rotten-spoons-reject.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/start": patch
3+
---
4+
5+
Reject server function calls when the response is a 5xx without an X-Error header, instead of resolving with the parsed error body

apps/tests/cypress/e2e/server-function.cy.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,14 @@ describe("server-function", () => {
5252
cy.visit("/generator-server-function");
5353
cy.get("#server-fn-test").contains('¡Hola, Mundo!');
5454
});
55+
it("should reject the promise when the response is a 5xx without an X-Error header", () => {
56+
cy.intercept("POST", "/_server*", {
57+
statusCode: 500,
58+
headers: { "content-type": "application/json" },
59+
body: { statusCode: 500, statusMessage: "boom" }
60+
});
61+
cy.visit("/server-function-rejected-on-500");
62+
cy.get("#call").click();
63+
cy.get("#server-fn-test").contains("rejected");
64+
});
5565
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createSignal } from "solid-js";
2+
import { serverFnWithIsServer } from "~/functions/use-is-server";
3+
4+
export default function App() {
5+
const [output, setOutput] = createSignal("pending");
6+
7+
const call = async () => {
8+
try {
9+
await serverFnWithIsServer();
10+
setOutput("resolved");
11+
} catch {
12+
setOutput("rejected");
13+
}
14+
};
15+
16+
return (
17+
<main>
18+
<button id="call" onClick={() => void call()}>
19+
call
20+
</button>
21+
<span id="server-fn-test">{output()}</span>
22+
</main>
23+
);
24+
}

packages/start/src/runtime/server-runtime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ async function fetchServerFunction(
8888
} else if (contentType && contentType.startsWith("application/json")) {
8989
result = await cloned.json();
9090
}
91-
if (response.headers.has("X-Error")) {
92-
throw result;
91+
if (response.headers.has("X-Error") || response.status >= 500) {
92+
throw result ?? new Error(`Server function call failed with status ${response.status}`);
9393
}
9494
return result;
9595
}

0 commit comments

Comments
 (0)