pexels / pexels-javascript

Official Javascript library for the Pexels API.
https://www.pexels.com/api/documentation/
148 stars 29 forks source link

throw an error when response != ok #23

Closed dorelljames closed 3 years ago

dorelljames commented 3 years ago

So I used an invalid API key and the API response is correct with 403, however, using the client here, it doesn't catch this as error. According to MDN, fetch's returned promise won't reject on HTTP error status. This patch checks and throws an Error when response is NOT ok!

Example code below:

client.photos.curated()
    .then((result) => console.log(result)) // always goes here
    .catch(err => console.error(err)) // never goes here
josephfarina commented 3 years ago

Hey @dorelljames, I think this is a great addition. Thank you for taking the time to submit a PR!