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 · 3 comments
Open

Allow action to throw error #182

qiushiyan opened this issue Jul 18, 2024 · 3 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(() => {})
}

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

3 participants