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

Allow action to throw error #182

Open
qiushiyan opened this issue Jul 18, 2024 · 5 comments
Open

Allow action to throw error #182

qiushiyan opened this issue Jul 18, 2024 · 5 comments

Comments

@qiushiyan
Copy link

It would be convenient to add a helper chaining method (e.g. .throw()) so that the server action would throw the error instead of returning it.

Use cases: the error.tsx and <ErrorBoundary /> components would automatically work if the server action throws the error, now I have to manually re-throw it

// page.tsx
const [data, err] = await action()
if (err) {
 throw new Error(err)
}

if with the chaining method I could just do

const action2 = action.throw()
const data = await action2()
@qiushiyan qiushiyan changed the title Add helper method to throw error Allow action to throw error Jul 18, 2024
@IdoPesok
Copy link
Owner

Hi, I am planning to improve error handling for the 1.0.0 release. Will make sure this functionality is available. In the meantime, you can try throwing the error in the shape error function on a base procedure.

@qiushiyan
Copy link
Author

Thanks for the amazing work!

@JoshuaKirby88
Copy link

Hey, I'm not really familiar with what errors can be.
In the experimental_shapeError callback, err is of type "unknown", whereas I thought all errors from zsa are of type TZSAError.
Can errors from experimental_shapeError be both an instance of the Error class, as well as TZSA?

"use server"

import { createServerActionProcedure, TAnyCompleteProcedure } from "zsa"

export const DALProcedure = (procedure: TAnyCompleteProcedure) => {
    return createServerActionProcedure(procedure)
        .experimental_shapeError(({ err }) => {
            const error = typeof err === "object" && err !== null && "data" in err && typeof err.data === "string" ? err.data : "Unknown database error"
            throw new Error(error)
        })
        .handler(() => {})
}

@Kamahl19
Copy link

@IdoPesok Hey, just wondering are you still working on this project or do you still plan to release v1? Anyway thanks for the great work.

@JoshuaKirby88
Copy link

@Kamahl19

I tried creating my own helper function for creating server actions and handling server action results and it's quite good since you can add a lot of custom behaviours like:

  • Instrument server action with Sentry (recording input and output).
  • Capture exception to Sentry on input parse error.
  • Automatically load i18n translations for the server action by passing the name of the server action.
  • Handling auth procedures.
  • Catching all errors and reporting to Sentry with the input, name of server action and auth procedure result.

And a function to handle the result on the client that:

  • Determines if the server action errored.
  • Render a toast automatically on error.

Let me know if you're interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants