pingdotgg / zact

Nothing to see here
https://zact-example.vercel.app
MIT License
983 stars 16 forks source link

Error handling #34

Open melkir opened 1 year ago

melkir commented 1 year ago

I got the following zact code.

export const sendCommand = zact(ActionSchema)(async ({ command, led }) => {
  const res = await fetch(`${process.env.NEXT_HOST_URL}/gpio/command`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ command, led }),
    cache: 'no-store',
  })
  return { status: res.status, statusText: res.statusText }
})

This command might fails and when it does, I got something like this

error Error: connect ECONNREFUSED 127.0.0.1:5002

However, currently the message isn't passed to the client, so I tried to do

 if (!res.ok) throw new Error(await res.text())

But it doesn't seems to work as wanted

const { mutate, error } = useZact(sendCommand)
// error is null