#Error handling best practices with Server Actions

14 messages · Page 1 of 1 (latest)

round stag
#

I'm using server actions directly as form actions in my project to benefit from progressive enhancement (i.e. the form works even without having JavaScript enabled). In this sort of a use case, what best practices are there for throwing and catching errors from the Server Action? For example, input validation errors.

  • I was initially thinking of error boundaries, but it seems that you can't really really include any information in the errors, as those are stripped out in prod
  • Another thought I had was redirecting back to the form, but with query parameters that indicate the error (e.g. ?error=noEmail)
  • Finally, I thought about returning data from the action, but as far as I know there's no way to consume this data without running JS on the client and calling the server action that way

Are there any best practices around this? Anyone handled a similar case in their project?

opaque rampartBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

round stag
#

Bump 🙂

round stag
#

Bumping this again

scarlet willow
#

There is no error handling for server actions yet. The React team is still working on it. If you call them outside of form actions I suggest returning Either monads in case you're familiar with them or alternatively simple responses using a discriminator like:

type ActionResponse =
  | {
      kind: 'success'
    }
  | {
      kind: 'error',
      description: '...'
      // w/e you need
    }
  ;
round stag
#

Yeah, calling them outside server actions would make it easy, but I'd like to keep the form working without JS as well

scarlet willow
#

In case of form actions old school query params are working just as fine like we did in PHP decades ago

round stag
#

😄

#

I have fond memories of that time, maybe it's time to embrace it again

#

glad to hear the React team has plans in this area though

#

looking forward to that

scarlet willow
#

There is nothing wrong with that approach I'd say

round stag
#

yeah, it works and is relatively low tech

#

thanks Near!