1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 09:23:13 +01:00

Simplify error handling in requests

This commit is contained in:
Brett Sun 2016-03-10 12:16:30 +01:00
parent 30ba85d937
commit 8952a607b7

View File

@ -13,11 +13,11 @@ class Requests {
unpackResponse(url) {
return (response) => {
if (response == null) {
throw new Error('For: ' + url + ' - Server did not respond to the request. (Not even displayed a 500)');
throw new Error(`For: ${url} - Server did not respond to the request. (Not even displayed a 500)`);
}
if (response.status >= 500) {
let err = new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url);
let err = new Error(`${response.status} - ${response.statusText} - on URL: ${response.url}`);
return response
.text()
@ -25,11 +25,11 @@ class Requests {
const resJson = JSON.parse(resText);
err = new Error(resJson.errors.pop());
// ES6 promises don't have a .finally() clause so
// we fake that here by forcing the .catch() clause
// to run
// ES6 promises don't have a .finally() clause so we fake that here by
// forcing the .catch() clause to run
return Promise.reject();
})
// If parsing the resText throws, just rethrow the original error we created
.catch(() => { throw err; });
}
@ -42,15 +42,16 @@ class Requests {
if (responseText && responseText !== 'None') {
const body = JSON.parse(responseText);
if(body && body.errors) {
let error = new Error('Form Error');
if (body && body.errors) {
const error = new Error('Form Error');
error.json = body;
reject(error);
} else if(body && body.detail) {
} else if (body && body.detail) {
reject(new Error(body.detail));
} else if('success' in body && !body.success) {
let error = new Error('Client Request Error');
} else if (body && 'success' in body && !body.success) {
const error = new Error('Client Request Error');
error.json = {
body: body,
status: response.status,
statusText: response.statusText,
type: response.type,
@ -60,13 +61,10 @@ class Requests {
} else {
resolve(body);
}
} else if (response.status >= 400) {
reject(new Error(`${response.status} - ${response.statusText} - on URL: ${response.url}`));
} else {
if(response.status >= 400) {
reject(new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url));
} else {
resolve({});
}
resolve({});
}
}).catch(reject);
});