T
TanStack2mo ago
genetic-orange

Server Fn error return types

Hello! In createServerFn functions, should we return normal typescript errors (ie. Error object) or http errors like return new Response('Internal Server Error', { status: 500 }) ? Thanks!
6 Replies
xenial-black
xenial-black2mo ago
you should throw errors those then need to be caught on the client side
genetic-orange
genetic-orangeOP2mo ago
thank you @Manuel Schiller , so all server fns result with an HTTP 200 on the client side right?
xenial-black
xenial-black2mo ago
no, you will get a 500 you can change this be setting setResponseStatus before throwing
genetic-orange
genetic-orangeOP2mo ago
I tried to make use of setResponseStatus but it did not work. And then, I followed the serialization-adapters example but again no luck. It keeps sending HTTP200 all the time.
xenial-black
xenial-black2mo ago
are you throwing the error?
genetic-orange
genetic-orangeOP2mo ago
yes, like:
setResponseStatus(499)
throw new Error('Invalid input', { foo: 'bar', bar: BigInt(123) })

or

setResponseStatus(500)
throw new Error('database error')
setResponseStatus(499)
throw new Error('Invalid input', { foo: 'bar', bar: BigInt(123) })

or

setResponseStatus(500)
throw new Error('database error')

Did you find this page helpful?