Just an extension to fetch which makes it reject on HTTP error codes and throw an appropiate error.
Install with npm i @rodrigoehlers/fetch.
Simply import / require the module in your code and use it as you'd use fetch with the simple difference that the returned Promise will reject if the response contains an HTTP status in the range of 4XX - 5XX.
import fetch from '@rodrigoehlers/fetch';
fetch('https://httpstat.us/500')
.then(() => console.log('successful'))
.catch(error => console.log(error));You'll see this in the console:
{
code: 500,
status: 'Bad Request',
body: ... // Optional response body
}This module depends on fetch and therefore won't natively work in nodejs.
You can check browser support here (MDN) or here (caniuse.com).
MIT - Made by Rodrigo Ehlers