PRPCClientError

API for the PRPCClientError instance

This is the error instance that pRPC will throw when ever it faces any issue.

Assuming you have this query defined:

export const add = query$(
  ({ payload }) => {
    const result = payload.a + payload.b
    return response$(
      { result },
      {
        headers: {
          'set-cookie': 'solid-testing=1',
        },
      }
    )
  },
  'add',
  z.object({
    a: z.number().max(5),
    b: z.number().max(10),
  })
)

You can handle errors thrown by zod on the client side like this:

const addRes = add(
  () => ({
    a: num1(),
    b: 3,
  }),
  () => ({
    placeholderData: (prev) => prev,
    onError: (error) => {
      if (error.isZodError()) {
        const fieldErrors = error.cause.fieldErrors
        console.log(fieldErrors.a)
        console.log(fieldErrors.b)
      }
    },
    retry: false,
  })
)

Using error.isZodError() you can check if the error is a zod error and then you can access the fieldErrors property to get the errors for each field, in a type safe way ofc.

You can also check if the error is a native Error instance using error.isError(), this will give you access to its message property.