Skip to content

rxMutation: clear mutationError automatically when a mutation is re-triggered (align with rxResource behavior) #273

@5im0n

Description

@5im0n

Hello teams !

First of all, thank you for the work on mutations. The API is very helpful and fits well with the Signal mental model.
I’d like to raise a question / suggestion regarding error handling when a mutation is re-triggered.

Expected / desired behavior

It would be very useful if mutationError were automatically cleared when a mutation is re-triggered, similarly to how rxResource clears its error when a new request starts.

In many UIs, the common expectation is:

  • An error is shown when a mutation fails
  • When the user retries the action, the previous error disappears and a new error is shown only if the retry fails again

This behavior is already implemented in rxResource, where starting a new load clears the previous error.

I use linkedSignal to bypass the problem

export default class LoginPage {
  readonly userService = inject(UserService);
  readonly router = inject(Router);

  protected readonly loginForm = form(signal(defaultLoginFormValue), loginFormSchema);


  protected mutation = rxMutation({
    onSuccess: () => {
      void router.navigate(['/'], { replaceUrl: true });
    },
    operation: (data: { email: string; password: string }) => userService.login(data),
  });

  protected readonly error = linkedSignal(() => {
    const httpErrorResponse = this.mutation.error() as HttpErrorResponseWithReason | undefined;

    return httpErrorResponse ? httpErrorResponse.error.reason : undefined;
  });

  login(event: SubmitEvent) {
    event.preventDefault();

    if (this.loginForm().valid()) {
      this.error.set(undefined);
      void this.mutation(this.loginForm().value());
    }
  }
}

Thank You

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions