From 5d410352dbebe3db53f3f261469d09a1791fa1ac Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 23 Sep 2016 12:24:23 +0100 Subject: [PATCH] Better error handling for fetch requests --- public/assets/js/newnote.js | 21 +++++++++++---------- resources/assets/js/newnote.js | 23 ++++++++++++----------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/public/assets/js/newnote.js b/public/assets/js/newnote.js index 54d2edd5..0ea0874b 100644 --- a/public/assets/js/newnote.js +++ b/public/assets/js/newnote.js @@ -12,11 +12,11 @@ if ('geolocation' in navigator) { function getLocation() { navigator.geolocation.getCurrentPosition(function (position) { //the locate button has been clicked so add the places/map - addPlaces(position.coords.latitude, position.coords.longitude); + addPlacesMap(position.coords.latitude, position.coords.longitude); }); } -function addPlaces(latitude, longitude) { +function addPlacesMap(latitude, longitude) { //get the nearby places fetch('/places/near/' + latitude + '/' + longitude, { credentials: 'same-origin', @@ -24,6 +24,10 @@ function addPlaces(latitude, longitude) { }).then(function (response) { return response.json(); }).then(function (j) { + if (j.error == true) { + alertify.reset(); + alertify.error(j.error_description); + } if (j.length > 0) { var i; var places = []; @@ -195,17 +199,13 @@ function addMap(latitude, longitude, places) { method: 'post', body: formData }) - .then(function (response) { - if (response.status >= 200 && response.status < 300) { - return Promise.resolve(response); - } else { - return Promise.reject(new Error(response.statusText)); - } - }) .then(function (response) { return response.json(); }) .then(function (placeJson) { + if (placeJson.error == true) { + throw new Error(placeJson.error_description); + } //create the slug from the url var urlParts = placeJson.split('/'); var slug = urlParts.pop(); @@ -247,7 +247,8 @@ function addMap(latitude, longitude, places) { //make selected selectPlace(slug); }).catch(function (placeError) { - console.error(placeError); + alertify.reset(); + alertify.error(placeError); }); }); }); diff --git a/resources/assets/js/newnote.js b/resources/assets/js/newnote.js index 54d2edd5..3e6d7792 100644 --- a/resources/assets/js/newnote.js +++ b/resources/assets/js/newnote.js @@ -1,4 +1,4 @@ -/* global L */ +/* global L, alertify */ if ('geolocation' in navigator) { var button = document.querySelector('#locate'); if (button.addEventListener) { @@ -12,11 +12,11 @@ if ('geolocation' in navigator) { function getLocation() { navigator.geolocation.getCurrentPosition(function (position) { //the locate button has been clicked so add the places/map - addPlaces(position.coords.latitude, position.coords.longitude); + addPlacesMap(position.coords.latitude, position.coords.longitude); }); } -function addPlaces(latitude, longitude) { +function addPlacesMap(latitude, longitude) { //get the nearby places fetch('/places/near/' + latitude + '/' + longitude, { credentials: 'same-origin', @@ -24,6 +24,10 @@ function addPlaces(latitude, longitude) { }).then(function (response) { return response.json(); }).then(function (j) { + if (j.error == true) { + alertify.reset(); + alertify.error(j.error_description); + } if (j.length > 0) { var i; var places = []; @@ -195,17 +199,13 @@ function addMap(latitude, longitude, places) { method: 'post', body: formData }) - .then(function (response) { - if (response.status >= 200 && response.status < 300) { - return Promise.resolve(response); - } else { - return Promise.reject(new Error(response.statusText)); - } - }) .then(function (response) { return response.json(); }) .then(function (placeJson) { + if (placeJson.error == true) { + throw new Error(placeJson.error_description); + } //create the slug from the url var urlParts = placeJson.split('/'); var slug = urlParts.pop(); @@ -247,7 +247,8 @@ function addMap(latitude, longitude, places) { //make selected selectPlace(slug); }).catch(function (placeError) { - console.error(placeError); + alertify.reset(); + alertify.error(placeError); }); }); });