Better error handling for fetch requests
This commit is contained in:
parent
32af94fab8
commit
5d410352db
2 changed files with 23 additions and 21 deletions
|
@ -12,11 +12,11 @@ if ('geolocation' in navigator) {
|
||||||
function getLocation() {
|
function getLocation() {
|
||||||
navigator.geolocation.getCurrentPosition(function (position) {
|
navigator.geolocation.getCurrentPosition(function (position) {
|
||||||
//the locate button has been clicked so add the places/map
|
//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
|
//get the nearby places
|
||||||
fetch('/places/near/' + latitude + '/' + longitude, {
|
fetch('/places/near/' + latitude + '/' + longitude, {
|
||||||
credentials: 'same-origin',
|
credentials: 'same-origin',
|
||||||
|
@ -24,6 +24,10 @@ function addPlaces(latitude, longitude) {
|
||||||
}).then(function (response) {
|
}).then(function (response) {
|
||||||
return response.json();
|
return response.json();
|
||||||
}).then(function (j) {
|
}).then(function (j) {
|
||||||
|
if (j.error == true) {
|
||||||
|
alertify.reset();
|
||||||
|
alertify.error(j.error_description);
|
||||||
|
}
|
||||||
if (j.length > 0) {
|
if (j.length > 0) {
|
||||||
var i;
|
var i;
|
||||||
var places = [];
|
var places = [];
|
||||||
|
@ -195,17 +199,13 @@ function addMap(latitude, longitude, places) {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
body: formData
|
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) {
|
.then(function (response) {
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then(function (placeJson) {
|
.then(function (placeJson) {
|
||||||
|
if (placeJson.error == true) {
|
||||||
|
throw new Error(placeJson.error_description);
|
||||||
|
}
|
||||||
//create the slug from the url
|
//create the slug from the url
|
||||||
var urlParts = placeJson.split('/');
|
var urlParts = placeJson.split('/');
|
||||||
var slug = urlParts.pop();
|
var slug = urlParts.pop();
|
||||||
|
@ -247,7 +247,8 @@ function addMap(latitude, longitude, places) {
|
||||||
//make selected
|
//make selected
|
||||||
selectPlace(slug);
|
selectPlace(slug);
|
||||||
}).catch(function (placeError) {
|
}).catch(function (placeError) {
|
||||||
console.error(placeError);
|
alertify.reset();
|
||||||
|
alertify.error(placeError);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* global L */
|
/* global L, alertify */
|
||||||
if ('geolocation' in navigator) {
|
if ('geolocation' in navigator) {
|
||||||
var button = document.querySelector('#locate');
|
var button = document.querySelector('#locate');
|
||||||
if (button.addEventListener) {
|
if (button.addEventListener) {
|
||||||
|
@ -12,11 +12,11 @@ if ('geolocation' in navigator) {
|
||||||
function getLocation() {
|
function getLocation() {
|
||||||
navigator.geolocation.getCurrentPosition(function (position) {
|
navigator.geolocation.getCurrentPosition(function (position) {
|
||||||
//the locate button has been clicked so add the places/map
|
//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
|
//get the nearby places
|
||||||
fetch('/places/near/' + latitude + '/' + longitude, {
|
fetch('/places/near/' + latitude + '/' + longitude, {
|
||||||
credentials: 'same-origin',
|
credentials: 'same-origin',
|
||||||
|
@ -24,6 +24,10 @@ function addPlaces(latitude, longitude) {
|
||||||
}).then(function (response) {
|
}).then(function (response) {
|
||||||
return response.json();
|
return response.json();
|
||||||
}).then(function (j) {
|
}).then(function (j) {
|
||||||
|
if (j.error == true) {
|
||||||
|
alertify.reset();
|
||||||
|
alertify.error(j.error_description);
|
||||||
|
}
|
||||||
if (j.length > 0) {
|
if (j.length > 0) {
|
||||||
var i;
|
var i;
|
||||||
var places = [];
|
var places = [];
|
||||||
|
@ -195,17 +199,13 @@ function addMap(latitude, longitude, places) {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
body: formData
|
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) {
|
.then(function (response) {
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then(function (placeJson) {
|
.then(function (placeJson) {
|
||||||
|
if (placeJson.error == true) {
|
||||||
|
throw new Error(placeJson.error_description);
|
||||||
|
}
|
||||||
//create the slug from the url
|
//create the slug from the url
|
||||||
var urlParts = placeJson.split('/');
|
var urlParts = placeJson.split('/');
|
||||||
var slug = urlParts.pop();
|
var slug = urlParts.pop();
|
||||||
|
@ -247,7 +247,8 @@ function addMap(latitude, longitude, places) {
|
||||||
//make selected
|
//make selected
|
||||||
selectPlace(slug);
|
selectPlace(slug);
|
||||||
}).catch(function (placeError) {
|
}).catch(function (placeError) {
|
||||||
console.error(placeError);
|
alertify.reset();
|
||||||
|
alertify.error(placeError);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue