EastCoastGreenwayAlliance / ecg-map

Interactive map and trip planner for the ECGA
https://map.greenway.org
7 stars 0 forks source link

Fetch API handling of response status !== 'OK' #63

Closed clhenrick closed 7 years ago

clhenrick commented 7 years ago

TIL that the fetch API won't reject a Promise if the response status is not within the success range. However, it does include an ok boolean in the response which can be used to detect server side errors.

Basically the error handling should be similar to:

fetch("http://httpstat.us/500")
    .then(function(response) {
        if (!response.ok) {
            throw Error(response.statusText);
        }
        return response;
    }).then(function(response) {
        console.log("ok");
    }).catch(function(error) {
        console.log(error);
    });

See this blog post for more: https://www.tjvantoll.com/2015/09/13/fetch-and-errors/

This update applies to the Mailchimp API calls as well as the Geocode API calls.