fetch-errors
WIP - nothing to see here
Last updated 9 months ago by bret .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install fetch-errors 
SYNC missed versions from official npm registry.

fetch-errors

Actions Status

Error subclasses for Text and JSON fetch response bodies, and a handleResponse fuction to handle fetch responses cleanly.

npm install fetch-errors

Usage

const {
  HTTPError,
  TextHTTPError,
  JSONHTTPError,
  handleResponse
} = require('fetch-errors');

window.fetch('https://api.github.com/users/bcomnes/repos')
  .then(handleResponse)
  .then(json => { console.log(json); })
  .catch(err => {
    switch (err.constructor) {
      case JSONHTTPError: {
        console.error(err.message);
        console.error(err.status);
        console.error(err.json);
        console.error(err.stack);
        break;
      }
      case TextHTTPError: {
        console.error(err.message);
        console.error(err.status);
        console.error(err.data);
        console.error(err.stack);
        break;
      }
      case HTTPError: {
        console.error(err.message);
        console.error(err.status);
        console.error(err.stack);
        break;
      }
      default: {
        console.error(err);
        break;
      }
    }
  });

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

License

MIT

Current Tags

  • 2.0.1                                ...           latest (9 months ago)

4 Versions

  • 2.0.1                                ...           9 months ago
  • 2.0.0                                ...           9 months ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (0)
None
Dev Dependencies (6)

Copyright 2014 - 2017 © taobao.org |