fetch-errors
Error subclasses for Text and JSON fetch
response bodies, and a handleResponse
fuction to handle fetch responses cleanly.
npm install fetch-errors
Usage
constHTTPErrorTextHTTPErrorJSONHTTPErrorhandleResponse} = ;window;
API
async handleResponse(response)
Parse JSON or text bodies of fetch
Response
objects. Intended for APIs that return JSON, but falls back to response.text()
body reading when the json
Content-type
is missing. If response.ok
, returns a JS object (if JSON), or the text content of the response body. Otherwise, returns a JSONHTTPError
or TextHTTPError
. If if response.json()
or resonse.text()
will throw their native error objects.
class HTTPError extends Error
Additional error properties from Error
stack // stack trace of error status // status code of response
class TextHTTPError extends HTTPError
Additional error properties from HTTPError
data // data of text response
class JSONHTTPError extends HTTPError
Additional error properties from HTTPError
json // json of a JSON response
See also
- netlify/micro-api-client: These errors were extracted from netlify/micro-api-client for individual use.
License
MIT