{"id":15497526,"url":"https://github.com/bcomnes/fetch-errors","last_synced_at":"2025-04-22T21:45:09.559Z","repository":{"id":57234563,"uuid":"222767427","full_name":"bcomnes/fetch-errors","owner":"bcomnes","description":"Error subclasses for Text and JSON fetch response bodies, and a handleResponse fuction to handle fetch responses cleanly.","archived":false,"fork":false,"pushed_at":"2020-02-10T20:13:20.000Z","size":14,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T19:11:12.310Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.com/fetch-errors","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bcomnes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-19T18:56:33.000Z","updated_at":"2021-07-15T05:11:04.000Z","dependencies_parsed_at":"2022-09-15T04:10:20.053Z","dependency_job_id":null,"html_url":"https://github.com/bcomnes/fetch-errors","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Ffetch-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Ffetch-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Ffetch-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Ffetch-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcomnes","download_url":"https://codeload.github.com/bcomnes/fetch-errors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250330517,"owners_count":21412991,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T08:39:22.170Z","updated_at":"2025-04-22T21:45:09.516Z","avatar_url":"https://github.com/bcomnes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fetch-errors\n[![Actions Status](https://github.com/bcomnes/fetch-errors/workflows/tests/badge.svg)](https://github.com/bcomnes/fetch-errors/actions)\n\nError subclasses for Text and JSON `fetch` response bodies, and a `handleResponse` fuction to handle fetch responses cleanly.\n\n```\nnpm install fetch-errors\n```\n\n## Usage\n\n``` js\nconst {\n  HTTPError,\n  TextHTTPError,\n  JSONHTTPError,\n  handleResponse\n} = require('fetch-errors');\n\nwindow.fetch('https://api.github.com/users/bcomnes/repos')\n  .then(handleResponse)\n  .then(json =\u003e { console.log(json); })\n  .catch(err =\u003e {\n    switch (err.constructor) {\n      case JSONHTTPError: {\n        console.error(err.message);\n        console.error(err.status);\n        console.error(err.json);\n        console.error(err.stack);\n        break;\n      }\n      case TextHTTPError: {\n        console.error(err.message);\n        console.error(err.status);\n        console.error(err.data);\n        console.error(err.stack);\n        break;\n      }\n      case HTTPError: {\n        console.error(err.message);\n        console.error(err.status);\n        console.error(err.stack);\n        break;\n      }\n      default: {\n        console.error(err);\n        break;\n      }\n    }\n  });\n```\n\n## API\n\n### `async handleResponse(response)`\n\nParse JSON or text bodies of [`fetch`][fetch] [`Response`][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.\n\n### `class HTTPError extends Error`\n\nAdditional error properties from Error\n\n```js\n{\n  stack, // stack trace of error\n  status // status code of response\n}\n```\n\n### `class TextHTTPError extends HTTPError`\n\nAdditional error properties from HTTPError\n\n```js\n{\n  data // data of text response\n}\n```\n\n### `class JSONHTTPError extends HTTPError`\n\nAdditional error properties from HTTPError\n\n```js\n{\n  json // json of a JSON response\n}\n```\n\n### See also\n\n- [netlify/micro-api-client](https://github.com/netlify/micro-api-client): These errors were extracted from netlify/micro-api-client for individual use.\n\n\n## License\n\nMIT\n\n[response]: https://developer.mozilla.org/en-US/docs/Web/API/Response\n[fetch]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Ffetch-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcomnes%2Ffetch-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Ffetch-errors/lists"}