blob: 880c56c9c2585d53b7ce1d858fbc9e3cfc73c6d1 [file] [log] [blame]
/** @module common/jsonOrThrow */
/** Helper function when making fetch() requests.
*
* Checks if the response is ok and converts it to JSON, otherwise it throws.
*
* @example
*
* fetch('/_/list').then(jsonOrThrow).then(json => {
* // Do something with the parsed json here.
* }).catch(errorMessage);
*
* @returns {Promise}
*/
export function jsonOrThrow(resp) {
if (resp.ok) {
return resp.json();
}
throw 'Bad network response.';
}