Created
April 24, 2020 15:36
-
-
Save NyaGarcia/866292edb3283c49b1e31f9fdd55471d to your computer and use it in GitHub Desktop.
Error handling with fromFetch()
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
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