You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to setup a basic form in a Start project, and was wondering what's the best way to gracefully handle errors thrown by the server function.
I want to start the simplest way possible, and then upgrade to the TanStack Form validation on the server + client later. But I guess this is more of a general question on how to deal with errors thrown from a server function.
When I throw json({ message: "Invalid form data" }, { status: 400 });
I can catch it in the client, but the error is not typed. When I log it, I see this output:
{"message":"Request failed with status 400","body":{"message":"Invalid form data"}}
Is there a way to infer the type of the error based on the server function return type? Or whats the recommended way to handle these?
Here's an example page:
import{useForm}from'@tanstack/react-form';import{createFileRoute}from'@tanstack/react-router'import{createServerFn,json}from'@tanstack/start';consthandleForm=createServerFn({method: "POST"}).validator((data: unknown)=>{console.log('data',data)if(!(datainstanceofFormData)){throwjson({message: "Invalid form data"},{status: 400});}constfirst_name=data.get("first_name");constlast_name=data.get("last_name");constemail=data.get("email");if(!first_name||!last_name||!email){throwjson({message: "All fields are required"},{status: 422});}return{first_name: first_name.toString(),last_name: last_name.toString(),email: email.toString(),};}).handler(async({ data })=>{console.log("data",data);return"Form has validated successfully";});exportconstRoute=createFileRoute('/form')({component: FormPage,})functionFormPage(){constform=useForm({onSubmit: async({ value })=>{console.log('data',value)try{constresponse=awaithandleForm({data: value});console.log(response);alert("Success!")}catch(e: unknown){console.log("error",e?.message);console.error(e);}},})return(<divclassName="p-2"><h3>Form!!!</h3><formonSubmit={(e)=>{e.preventDefault();e.stopPropagation();form.handleSubmit();}}><inputname="first_name"placeholder="First Name"/><inputname="last_name"placeholder="Last Name"/><inputname="email"placeholder="Email"/><buttontype="submit">Submit</button></form></div>)}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Total beginner with TanStack here.
I was trying to setup a basic form in a Start project, and was wondering what's the best way to gracefully handle errors thrown by the server function.
I want to start the simplest way possible, and then upgrade to the TanStack Form validation on the server + client later. But I guess this is more of a general question on how to deal with errors thrown from a server function.
When I
throw json({ message: "Invalid form data" }, { status: 400 });
I can catch it in the client, but the error is not typed. When I log it, I see this output:
Is there a way to infer the type of the error based on the server function return type? Or whats the recommended way to handle these?
Here's an example page:
Beta Was this translation helpful? Give feedback.
All reactions