Created
July 27, 2021 13:47
-
-
Save FunnyGhost/6e538b1787e136afa7a87110c24a405b to your computer and use it in GitHub Desktop.
MovieService with StatusCodeResponseService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Optional } from '@angular/core'; | |
import { Observable } from 'rxjs'; | |
import { StatusCodeResponseService } from './status-code-response.service'; | |
import { HttpClient, HttpErrorResponse } from '@angular/common/http'; | |
import { environment } from '@env/environment'; | |
import { Observable, throwError } from 'rxjs'; | |
import { catchError } from 'rxjs/operators'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class MovieService { | |
constructor(@Optional() private statusCodeResponseService?: StatusCodeResponseService, private httpClient: HttpClient) {} | |
getMovieById(id: string): Observable<Movie> { | |
return this.httpClient.get<Movie>(environment.moviesBaseUrl).pipe( | |
catchError((error) => { | |
if (error instanceof HttpErrorResponse && error.statusCode === 404) { | |
this.statusCodeResponseService?.setNotFound('Movie does not exist...yet!'); | |
return EMPTY; | |
} | |
return throwError(error); | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment