├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Matan Kotler-Berkowitz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fetch Reject 2 | >A simple wrapper for fetch which rejects on HTTP error 3 | 4 | Normal `fetch` does not throw on HTTP error (non-200 status code). This wrapper allows it to do so. 5 | ## Install 6 | ``` 7 | $ npm install --save fetch-reject; 8 | ``` 9 | ## Usage 10 | ```js 11 | import fetch from 'fetch-reject'; 12 | 13 | // use just like normal fetch 14 | fetch('http://httpstat.us/500') 15 | .then(date => doSomething(data)); 16 | .catch(err => { 17 | console.log(err) // -> 'GET ... 500 (Internal Server Error)' 18 | console.log(err.status) // -> 500 19 | }) 20 | 21 | ``` 22 | ## API 23 | 24 | See the [`fetch` docs](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) for more. 25 | 26 | ### fetch(url, [init]) 27 | 28 | Same as normal `fetch`, but it rejects with an [`Error` object](#Returns) on non-200. 29 | 30 | #### Returns 31 | 32 | Type: `Error` 33 | 34 | Will be `reject`-ed on non-200 response. An error object containing the `statusText` of the response. Has a `status` field which is equal to `response.status`; 35 | 36 | ### createFetchReject(originFetch) 37 | 38 | Returns a `fetch` function similar to the one documented above, but using the provided `originFetch` function. Can be used to enhance polyfills. 39 | 40 | ### originFetch 41 | 42 | Type: `function` 43 | 44 | A function that complies with the original `fetch` spec. Will be used instead of `window.fetch` internally 45 | 46 | #### Returns 47 | Type: `function` 48 | 49 | A function that acts similarly to `fetch` (detailed above), but uses the provided `originFetch` function instead. 50 | ## License 51 | 52 | [MIT © Matan Kotler-Berkowitz](https://github.com/matankb/fetch-reject/blob/master/LICENSE) 53 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function handleErrors(response) { 2 | if (response.ok) { 3 | return response; 4 | } 5 | 6 | var error = new Error(); 7 | // attatch status and response to error so it can be accessed by handler 8 | error.status = response.status; 9 | error.response = response; 10 | throw error; 11 | 12 | } 13 | 14 | function fetchReject(url, init) { 15 | return fetch(url, init) 16 | .then(handleErrors) 17 | } 18 | 19 | function createFetchReject(originFetch) { 20 | // function to enhance fetch polyfills 21 | return function(url, init) { 22 | originFetch(url, init) 23 | .then(handleErrors); 24 | } 25 | } 26 | 27 | module.exports = fetchReject; 28 | module.exports.createFetchReject = createFetchReject; 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fetch-reject", 3 | "version": "1.3.0", 4 | "description": "A simple wrapper for fetch which rejects on HTTP error", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/matankb/fetch-reject.git" 12 | }, 13 | "keywords": [ 14 | "fetch" 15 | ], 16 | "author": "Matan Kotler-Berkowitz", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/matankb/fetch-reject/issues" 20 | }, 21 | "homepage": "https://github.com/matankb/fetch-reject#readme" 22 | } 23 | --------------------------------------------------------------------------------