Module: common-sk/modules/jsonOrThrow

Methods

(static) jsonOrThrow() → {Promise}

Helper function when making fetch() requests. Checks if the response is ok and converts it to JSON, otherwise it throws.
Source:
Throws:
with status, resp, and message elements. See the Response docs for more detail on reading resp (e.g. resp.text()).
Type
Object
Returns:
Type
Promise
Example
fetch('/_/list').then(jsonOrThrow).then((json) => {
     // Do something with the parsed json here.
   }).catch((r) => {
     if (r.status === 403) {
       // Handle HTTP response 403 - not authorized here.
     } else {
       console.err(r.message);
     }
 }
   });
 });