IdoPesok / zsa

https://zsa.vercel.app
MIT License
436 stars 13 forks source link

ZSA is catching the error thrown internally by redirect function in NextJS #127

Closed sarthak61199 closed 2 weeks ago

sarthak61199 commented 2 weeks ago

Hi,

When using redirect function in a server action made using ZSA library, it catches the error thrown internally in redirect and returns an error even if the server action was successful. I even tried moving the redirect function call to onSuccess callback but I got the same result.

Reference -> https://nextjs.org/docs/app/building-your-application/routing/redirecting

IdoPesok commented 2 weeks ago

Hi, what version of ZSA are you using? This was patched in later versions

sarthak61199 commented 2 weeks ago

Hi,

Thanks for the quick response , I'm using the following zsa versions ->

"zsa": "^0.3.4", "zsa-react-query": "^0.1.0"

IdoPesok commented 2 weeks ago

Those versions should be good. For reference, here is the line that checks for redirects or not founds:

if (err.message === "NEXT_REDIRECT" || err.message === "NEXT_NOT_FOUND")

I wonder if your redirect error maybe has a different message that we aren't catching? Do you have a log of the final error you are getting returned? Curious what the message is in that error.

sarthak61199 commented 2 weeks ago

Hi,

I just did some digging. Here are some findings ->

-> I printed the error on console and got "TypeError: undefined has no properties" -> The error seems to be coming in the following code block

 const useServerActionMutation = (action, options, queryClient) => {
    return useMutation(
      {
        ...options,
        mutationFn: async (...args2) => {
          const [data, err] = await action(...args2);
          if (options?.returnError) {
            return [data, err];
          }
          if (err) {
            throw err;
          }
          return data;
        }
      },
      queryClient
    );
  };
IdoPesok commented 2 weeks ago

Thanks for catching this! Seemed like it was only an issue with react query mutations. Please give zsa-react-query@0.1.1 a shot 🙏

sarthak61199 commented 2 weeks ago

Thanks. Ill give it a try and report back

sarthak61199 commented 2 weeks ago

It's working now, thank you so much for this amazing library!

IdoPesok commented 2 weeks ago

Awesome 🫡 Happy to help. If you find any more issues please make an issue and will fix ASAP.