From 611ecaadc1a13e6d2b0b5fba89fefdfb99bdeb6c Mon Sep 17 00:00:00 2001 From: Ashish Khanal Date: Wed, 3 Jan 2024 00:06:27 -0500 Subject: [PATCH] Add some comments to error handling logic --- src/app/hero.service.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts index c0e72bc..15328f2 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -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('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(operation = 'operation', result?: T){ return (error: any): Observable => { @@ -102,4 +103,8 @@ export class HeroService { return of(emptyResult); } } + + private log(message: string): void { + this.messageService.add(`HeroService: ${message}`); + } }