I want to handle http error messages and show them using Angular material snackbar module within the service. I don't want to do all this repetitive work in the components. How can I do it?
I get the following error: ERROR TypeError: this.openSnackBar is not a function . Note that it works fine in the component.
private handleError(error: HttpErrorResponse) {
let message:string = "Something bad happened; please try again later."
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong.
if(error.status == 404){
this.openSnackBar('No data found!','Dismiss')
}
}
console.log(error)
// Return an observable with a user-facing error message.
return throwError(message);
}
openSnackBar(message: string, action: string) {
this._snackBar.open(message, action, {
duration: 2000,
});
}