From 1b6f2803713f8e914085d6c626887d18a90cc44f Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Fri, 23 Oct 2020 20:49:37 -0400 Subject: [PATCH] don't call user/new if user/me returns 500 --- dist/bundle.es.js | 17 ++++++++++++++--- src/lbryio.js | 35 ++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/dist/bundle.es.js b/dist/bundle.es.js index 02e2e57..aff6e48 100644 --- a/dist/bundle.es.js +++ b/dist/bundle.es.js @@ -214,7 +214,8 @@ const Lbryio = { exchangeLastFetched: null, CONNECTION_STRING: 'https://api.lbry.com/' }; -const EXCHANGE_RATE_TIMEOUT = 20 * 60 * 1000; // We can't use env's because they aren't passed into node_modules +const EXCHANGE_RATE_TIMEOUT = 20 * 60 * 1000; +const INTERNAL_APIS_DOWN = 'internal_apis_down'; // We can't use env's because they aren't passed into node_modules Lbryio.setLocalApi = endpoint => { Lbryio.CONNECTION_STRING = endpoint.replace(/\/*$/, '/'); // exactly one slash at the end; @@ -234,7 +235,11 @@ Lbryio.call = (resource, action, params = {}, method = 'get') => { return response.json(); } - return response.json().then(json => { + if (response.status === 500) { + return Promise.reject(INTERNAL_APIS_DOWN); + } + + if (response) return response.json().then(json => { let error; if (json.error) { @@ -337,7 +342,13 @@ Lbryio.authenticate = () => { } // check that token works - return Lbryio.getCurrentUser().then(user => user).catch(() => false); + return Lbryio.getCurrentUser().then(user => user).catch(error => { + if (error === INTERNAL_APIS_DOWN) { + throw new Error('Internal APIS down'); + } + + return false; + }); }).then(user => { if (user) { return user; diff --git a/src/lbryio.js b/src/lbryio.js index bd44a35..0b3d2e0 100644 --- a/src/lbryio.js +++ b/src/lbryio.js @@ -11,6 +11,7 @@ const Lbryio = { }; const EXCHANGE_RATE_TIMEOUT = 20 * 60 * 1000; +const INTERNAL_APIS_DOWN = 'internal_apis_down'; // We can't use env's because they aren't passed into node_modules Lbryio.setLocalApi = endpoint => { @@ -30,16 +31,22 @@ Lbryio.call = (resource, action, params = {}, method = 'get') => { if (response.status >= 200 && response.status < 300) { return response.json(); } - 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); - }); + + if (response.status === 500) { + return Promise.reject(INTERNAL_APIS_DOWN); + } + + if (response) + 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 makeRequest(url, options) { @@ -129,7 +136,13 @@ Lbryio.authenticate = () => { // check that token works return Lbryio.getCurrentUser() .then(user => user) - .catch(() => false); + .catch(error => { + if (error === INTERNAL_APIS_DOWN) { + throw new Error('Internal APIS down'); + } + + return false; + }); }) .then(user => { if (user) {