connect-es icon indicating copy to clipboard operation
connect-es copied to clipboard

Allow extension or overwrite of error handling behaviour to allow custom errors to bubble up

Open p-mercury opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. I am using the connect RPC client in a Sveltekit applications and on the client transport I have an authentication interceptor. The interceptor manages the access token, adding it to the requests Authorization header if it is valid, but if the token is expired or not present the interceptor throws a redirect error as documented for Sveltekit.

The problem is that currently this redirect error gets converted to a connect error by the setupSignal function which prevents the Sveltekit behaviour of redirecting when this error is detected. This seems like a reasonable use case and is currently not possible.

Describe the solution you'd like I am not sure what the best way to implement this features is, but two options would be:

  • A transport option to prevent error conversion
  • A transport option that allows you to extend the error handling behaviour

Describe alternatives you've considered This is the only way I see this currently working, but this defeats the purpose of the interceptor if on every call I need to manually add error handling.

try {
    await client.identity.listAccounts({});
  } catch (err) {
    if (err instanceof ConnectError && isRedirect(err.cause)) {
      throw err.cause;
    }
    throw err;
  }

p-mercury avatar Feb 19 '25 13:02 p-mercury

It wasn't possible to use cause for a long time, but it's widely available now. We've filed https://github.com/sveltejs/kit/issues/13487, asking whether they're open to support cause when looking for a redirect error.

timostamm avatar Feb 24 '25 15:02 timostamm