Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Created April 24, 2020 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NyaGarcia/866292edb3283c49b1e31f9fdd55471d to your computer and use it in GitHub Desktop.
Save NyaGarcia/866292edb3283c49b1e31f9fdd55471d to your computer and use it in GitHub Desktop.
Error handling with fromFetch()
const ghibliFilmError$ = fromFetch("https://ghibliapi.herokuapp.com/wrongURL").pipe(
switchMap(response => {
if (!response.ok) {
throw `There was an error with status ${response.status}`;
}
return response.json();
}),
catchError(err => {
console.error(err);
return of({ error: true, message: err });
})
);
ghibliFilmError$.subscribe(result => console.log(result));
// Output: {error: true, message: "There was an error with status 404"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment