Last active
November 3, 2025 07:54
-
Star
(155)
You must be signed in to star a gist -
Fork
(32)
You must be signed in to fork a gist
-
-
Save odewahn/5a5eeb23279eed6a80d7798fdb47fe91 to your computer and use it in GitHub Desktop.
Revisions
-
odewahn revised this gist
Jun 25, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -18,4 +18,4 @@ fetch("/api/foo") }) ``` Frankly, I'm horrified that JavaScript let's you throw some random value, rather than an error, but hey, when in Rome... -
odewahn revised this gist
Jun 25, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ I really liked @tjvantoll article [Handling Failed HTTP Responses With fetch()](https://www.tjvantoll.com/2015/09/13/fetch-and-errors/). The one thing I found annoying with it, though, is that `response.statusText` always returns the generic error message associated with the error code. Most APIs, however, will generally return some kind of useful, more human friendly message in the body. Here's a modification that will capture this message. The key is that rather than throwing an error, you just throw the response and then process it in the `catch` block to extract the message in the body: ``` fetch("/api/foo") -
odewahn created this gist
Jun 25, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ I really liked @tjvantoll article [Handling Failed HTTP Responses With fetch()](https://www.tjvantoll.com/2015/09/13/fetch-and-errors/). The one thing I found annoyting with it, though, is that `response.statusText` always returns the generic error message associated with the error code. Most APIs, however, will generally return some kind of useful, more human friendly message in the body. Here's a modification that will capture this message. The key is that rather than throwing an error, you just trhow the response and then reprocess it in the `catch` block to extract the message in the body: ``` fetch("/api/foo") .then( response => { if (!response.ok) { throw response } return response.json() //we only get here if there is no error }) .then( json => { this.props.dispatch(doSomethingWithResult(json)) }) .catch( err => { err.text().then( errorMessage => { this.props.dispatch(displayTheError(errorMessage)) }) }) ``` Frankly, I was horrified and aghast that JavaScript let's you throw some random value, rather than an error, but hey, when in Rome...