From 7a9aaf377bbd269fa9338695263e193bde8a944f Mon Sep 17 00:00:00 2001 From: Jeremy Kauffman Date: Fri, 10 Nov 2017 09:55:30 -0500 Subject: [PATCH] fix API error propegation --- ui/js/actions/user.js | 2 +- ui/js/jsonrpc.js | 25 +++++++++++++------------ ui/js/lbryio.js | 25 +++++++++++++------------ 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/ui/js/actions/user.js b/ui/js/actions/user.js index 360707a43..05f361f4a 100644 --- a/ui/js/actions/user.js +++ b/ui/js/actions/user.js @@ -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", diff --git a/ui/js/jsonrpc.js b/ui/js/jsonrpc.js index f6c638083..d3cebcfda 100644 --- a/ui/js/jsonrpc.js +++ b/ui/js/jsonrpc.js @@ -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); diff --git a/ui/js/lbryio.js b/ui/js/lbryio.js index ece653546..ba7a105cd 100644 --- a/ui/js/lbryio.js +++ b/ui/js/lbryio.js @@ -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 => {