0

I am trying to improve my code by embracing code reuse.

  this.someService
      .updateItem({
        id: item?.id,
        body: itemBody,
      })
      .subscribe({
        next: () => {
          // handle success
        },
        error: (error: HttpErrorMessage): void {
               this.errorsService.display({
               message: error.message,
              });
       },
      });

I have discovered that this block of code

error: (error: HttpErrorMessage): void {
               this.errorsService.display({
               message: error.message,
              });
       },

is repeated a lot and I wanted to counter that by having a function like this

 private handleError(error: HttpErrorMessage): void {
     this.errorsService.display({
               message: error.message,
              });
  }

and I tried calling it like this this.handleError e.g

this.someService
      .updateItem({
        id: item?.id,
        body: itemBody,
      })
      .subscribe({
        next: () => {
          // handle success
        },
        error: this.handleError,
      });

I can see that the function gets invoked because I can log error.message and see the value . However i get an exception that this.errorsService is not defined

Kthree
  • 156
  • 1
  • 12

0 Answers0