Skip to content

Commit ef269bd

Browse files
committed
test: serve error (wip)
1 parent 0e0dd89 commit ef269bd

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

packages/plugin-rsc/examples/basic/src/framework/entry.rsc.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export async function handleRequest({
3939
let returnValue: RscPayload['returnValue'] | undefined
4040
let formState: ReactFormState | undefined
4141
let temporaryReferences: unknown | undefined
42+
let statusCode = 200
4243
if (isAction) {
4344
// x-rsc-action header exists when action is called via `ReactClient.setServerCallback`.
4445
const actionId = request.headers.get('x-rsc-action')
@@ -55,15 +56,20 @@ export async function handleRequest({
5556
returnValue = { ok: true, data }
5657
} catch (e) {
5758
returnValue = { ok: false, data: e }
59+
statusCode = 500
5860
}
5961
} else {
6062
// otherwise server function is called via `<form action={...}>`
6163
// before hydration (e.g. when javascript is disabled).
6264
// aka progressive enhancement.
6365
const formData = await request.formData()
6466
const decodedAction = await decodeAction(formData)
65-
const result = await decodedAction()
66-
formState = await decodeFormState(result, formData)
67+
try {
68+
const result = await decodedAction()
69+
formState = await decodeFormState(result, formData)
70+
} catch (e) {
71+
statusCode = 500
72+
}
6773
}
6874
}
6975

@@ -82,7 +88,7 @@ export async function handleRequest({
8288

8389
if (isRscRequest) {
8490
return new Response(rscStream, {
85-
status: returnValue?.ok === false ? 500 : undefined,
91+
status: statusCode,
8692
headers: {
8793
'content-type': 'text/x-component;charset=utf-8',
8894
vary: 'accept',
@@ -106,6 +112,7 @@ export async function handleRequest({
106112

107113
// respond html
108114
return new Response(htmlStream, {
115+
status: statusCode,
109116
headers: {
110117
'content-type': 'text/html;charset=utf-8',
111118
vary: 'accept',

packages/plugin-rsc/examples/basic/src/routes/root.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { TestHmrClientDep3 } from './hmr-client-dep3/server'
4747
import { TestChunk2 } from './chunk2/server'
4848
import { TestUseId } from './use-id/server'
4949
import { TestClientError } from './client-error/client'
50+
import { TestServerError } from './server-error/server'
5051

5152
export function Root(props: { url: URL }) {
5253
return (
@@ -80,6 +81,7 @@ export function Root(props: { url: URL }) {
8081
<TestTemporaryReference />
8182
<TestServerActionError />
8283
<TestClientError />
84+
<TestServerError url={props.url} />
8385
<TestReplayConsoleLogs url={props.url} />
8486
<TestSuspense url={props.url} />
8587
<TestActionFromClient />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export async function TestServerError(props: { url: URL }) {
2+
if (props.url.searchParams.has('test-server-error')) {
3+
throw new Error('test-server-error!')
4+
}
5+
return (
6+
<div>
7+
<span>test-server-error</span>{' '}
8+
<a href="?test-server-error">test-server-error</a>{' '}
9+
</div>
10+
)
11+
}

0 commit comments

Comments
 (0)