Skip to content

Instantly share code, notes, and snippets.

@FunnyGhost
Created July 27, 2021 13:47
Show Gist options
  • Save FunnyGhost/6e538b1787e136afa7a87110c24a405b to your computer and use it in GitHub Desktop.
Save FunnyGhost/6e538b1787e136afa7a87110c24a405b to your computer and use it in GitHub Desktop.
MovieService with StatusCodeResponseService
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