fix API error propegation

This commit is contained in:
Jeremy Kauffman 2017-11-10 09:55:30 -05:00
parent 4c94f65493
commit 7a9aaf377b
3 changed files with 27 additions and 25 deletions

View file

@ -69,7 +69,7 @@ export function doUserEmailNew(email) {
"post"
)
.catch(error => {
if (error.xhr && error.xhr.status == 409) {
if (error.response && error.response.status == 409) {
return lbryio.call(
"user_email",
"resend_token",

View file

@ -9,26 +9,28 @@ jsonrpc.call = function(
connectFailedCallback,
timeout
) {
function checkStatus(response) {
function checkAndParse(response) {
if (response.status >= 200 && response.status < 300) {
return response;
return response.json();
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
return response.json().then(json => {
let error;
if (json.error) {
error = new Error(json.error);
} else {
error = new Error("Protocol error with unknown response signature");
}
return Promise.reject(error);
});
}
}
function parseJSON(response) {
return response.json();
}
function makeRequest(url, options) {
return new Promise((resolve, reject) => {
fetch(url, options).then(resolve).catch(reject);
if (timeout) {
const e = new Error(__("XMLHttpRequest connection timed out"));
const e = new Error(__("Protocol request timed out"));
setTimeout(() => {
return reject(e);
}, timeout);
@ -51,8 +53,7 @@ jsonrpc.call = function(
sessionStorage.setItem("JSONRPCCounter", counter + 1);
return fetch(url, options)
.then(checkStatus)
.then(parseJSON)
.then(checkAndParse)
.then(response => {
const error =
response.error || (response.result && response.result.error);

View file

@ -45,24 +45,25 @@ lbryio.call = function(resource, action, params = {}, method = "get") {
return Promise.reject(new Error(__("Invalid method")));
}
function checkStatus(response) {
function checkAndParse(response) {
if (response.status >= 200 && response.status < 300) {
return response;
return response.json();
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
return response.json().then(json => {
let error;
if (json.error) {
error = new Error(json.error);
} else {
error = new Error("Unknown API error signature");
}
error.response = response; //this is primarily a hack used in actions/user.js
return Promise.reject(error);
});
}
}
function parseJSON(response) {
return response.json();
}
function makeRequest(url, options) {
return fetch(url, options).then(checkStatus).then(parseJSON).catch(e => {
throw e;
});
return fetch(url, options).then(checkAndParse);
}
return lbryio.getAuthToken().then(token => {