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

fix(start): Send error messages from server functions #2625

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
18 changes: 4 additions & 14 deletions packages/start/src/client-runtime/fetcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,10 @@ async function handleResponseErrors(response: Response) {
return await response.text()
})()

const message = `Request failed with status ${response.status}`

if (isJson) {
throw new Error(
JSON.stringify({
message,
body,
}),
)
} else {
throw new Error(
[message, `${JSON.stringify(body, null, 2)}`].join('\n\n'),
)
}
const defaultMessage = `Request failed with status ${response.status}`
const message = body?.message || body || defaultMessage

throw new Error(message)
}

return response
Expand Down
23 changes: 13 additions & 10 deletions packages/start/src/server-handler/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,20 @@ export async function handleServerRequest(request: Request, event?: H3Event) {
return redirectOrNotFoundResponse(error)
}

console.error('Server Fn Error!')
console.error(error)
console.info()

return new Response(JSON.stringify(error), {
status: 500,
headers: {
'Content-Type': 'application/json',
[serverFnReturnTypeHeader]: 'error',
return new Response(
error instanceof Error
? error.toString()
: JSON.stringify({
message: error?.message ?? 'Internal Server Error',
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
[serverFnReturnTypeHeader]: 'error',
},
},
})
)
}
})()

Expand Down