Skip to content

An extension to fetch which makes it reject on HTTP error codes and throw an appropiate error.

License

Notifications You must be signed in to change notification settings

rodrigoehlers/fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@rodrigoehlers/fetch

Just an extension to fetch which makes it reject on HTTP error codes and throw an appropiate error.

Installation

Install with npm i @rodrigoehlers/fetch.

Usage

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
}

Compatibility

This module depends on fetch and therefore won't natively work in nodejs.

You can check browser support here (MDN) or here (caniuse.com).

License

MIT - Made by Rodrigo Ehlers

About

An extension to fetch which makes it reject on HTTP error codes and throw an appropiate error.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published