Skip to content

Commit

Permalink
Add some comments to error handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashish Khanal committed Jan 3, 2024
1 parent b5b513a commit 611ecaa
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/app/hero.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ export class HeroService {
tap(heroes => heroes.length ?
this.log(`found heroes matching "${term}"`) :
this.log(`no heroes matching "${term}"`)),
// handleError is executed when an error occurs in the API call, and it returns a new function
// This new function is only called when catchError passes the error to it
// So 1.handleError call happens and 2. the function returned by handleError call happens
// Function returned by handleError provides the replacement Observable
catchError(this.handleError<Hero[]>('searchHeroes', []))
);
}

private log(message: string): void {
this.messageService.add(`HeroService: ${message}`);
}

// Learn more here: https://youtu.be/L9kFTps_7Tk?si=M6ZWbR71SD13DPk4&t=107
// This method returns an 'error handler' function that expects 'error: any` input parameter
private handleError<T>(operation = 'operation', result?: T){
return (error: any): Observable<T> => {
Expand All @@ -102,4 +103,8 @@ export class HeroService {
return of(emptyResult);
}
}

private log(message: string): void {
this.messageService.add(`HeroService: ${message}`);
}
}

0 comments on commit 611ecaa

Please sign in to comment.