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

Custom error serialization #492

Open
spotsccc opened this issue Jul 15, 2024 · 1 comment
Open

Custom error serialization #492

spotsccc opened this issue Jul 15, 2024 · 1 comment
Labels
good first issue Good for newcomers scope:core type:enhancement New feature or request
Milestone

Comments

@spotsccc
Copy link

spotsccc commented Jul 15, 2024

Hi! I'd like to use custom serialization for $error store in query and mutation.

My use case:

I have a project that uses nextjs and farfetched, sometimes a NetworkError can happen on the server side, this type of error has a non-serializable property "cause". In this situation I can't to serialize queries running on the server. Now the only one solution is to set serialize property to 'ignore', but it will disable serialization for all stores. That's not what i want.

My suggestion:

I think that it would be nice to separate serialization for $error and $data. As i understood, now serialization property works like this:

  1. If serialization equals to 'ignore', it will disable all serialization
  2. If serialization equals to {write: ..., read...}, this rules will be used only for $data store, and will have no effect for all other stores

And I suggest changing this behavior to this:
type Serialize<Data | Initial> = Serialize<Data | Initial> | {error?: Serialize<Data | Initial>, data?: Serialize<Data | Initial>} | undefined;

  1. If serialization equals to 'ignore' or {write: ..., read...}, it will works like before
  2. If serialization equals to {error: ..., data: ... }, it will apply different serialization rules for $error and $data

Hope my suggestion helps!

@igorkamyshev igorkamyshev added type:enhancement New feature or request good first issue Good for newcomers scope:core labels Jul 16, 2024
@igorkamyshev igorkamyshev added this to the v1.0 milestone Jul 16, 2024
@zerobias
Copy link
Collaborator

error.cause is almost always are non-serialable, so we probably could just add default serialization to $error store:

$error = createStore(null, {
  serialize: {
    read: error => error,
    write: ({cause, ...error}) => error,
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers scope:core type:enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants