From 619d6ffe963bb874c8fb57c8289cfaf1dc237fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Thu, 14 Jun 2018 13:05:09 +0200 Subject: [PATCH] Don't print potential HTML error that our server seems to return --- src/api/Ledger.js | 41 ----------------------------------------- src/api/network.js | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/api/Ledger.js b/src/api/Ledger.js index 722a90a8..2c84ab5d 100644 --- a/src/api/Ledger.js +++ b/src/api/Ledger.js @@ -1,48 +1,7 @@ // @flow import type { Currency } from '@ledgerhq/live-common/lib/types' -import logger from 'logger' -import createCustomErrorClass from 'helpers/createCustomErrorClass' const BASE_URL = process.env.LEDGER_REST_API_BASE || 'https://api.ledgerwallet.com/' -export const LedgerAPIErrorWithMessage = createCustomErrorClass('LedgerAPIErrorWithMessage') -export const LedgerAPIError = createCustomErrorClass('LedgerAPIError') -export const NetworkDown = createCustomErrorClass('NetworkDown') - -export const userFriendlyError = (p: Promise): Promise => - p.catch(error => { - if (error.response) { - // The request was made and the server responded with a status code - // that falls out of the range of 2xx - const { data } = error.response - if (data && typeof data.error === 'string') { - let msg = data.error || data.message - if (typeof msg === 'string') { - const m = msg.match(/^JsDefined\((.*)\)$/) - if (m) { - try { - const { message } = JSON.parse(m[1]) - if (typeof message === 'string') { - msg = message - } - } catch (e) { - logger.warn("can't parse server result", e) - } - } - throw new LedgerAPIErrorWithMessage(msg) - } - } - const { status } = error.response - logger.log('Ledger API: HTTP status', status, 'data: ', error.response.data) - throw new LedgerAPIError(`LedgerAPIError ${status}`, { status }) - } else if (error.request) { - // The request was made but no response was received - // `error.request` is an instance of XMLHttpRequest in the browser and an instance of - // http.ClientRequest in node.js - throw new NetworkDown() - } - throw error - }) - export const blockchainBaseURL = ({ ledgerExplorerId }: Currency): ?string => ledgerExplorerId ? `${BASE_URL}blockchain/v2/${ledgerExplorerId}` : null diff --git a/src/api/network.js b/src/api/network.js index 091f5fe4..b4bfb76d 100644 --- a/src/api/network.js +++ b/src/api/network.js @@ -1,8 +1,50 @@ // @flow import axios from 'axios' import { GET_CALLS_RETRY, GET_CALLS_TIMEOUT } from 'config/constants' -import { userFriendlyError } from 'api/Ledger' import { retry } from 'helpers/promise' +import logger from 'logger' +import createCustomErrorClass from 'helpers/createCustomErrorClass' + +export const LedgerAPIErrorWithMessage = createCustomErrorClass('LedgerAPIErrorWithMessage') +export const LedgerAPIError = createCustomErrorClass('LedgerAPIError') +export const NetworkDown = createCustomErrorClass('NetworkDown') + +const userFriendlyError = (p: Promise): Promise => + p.catch(error => { + if (error.response) { + // The request was made and the server responded with a status code + // that falls out of the range of 2xx + const { data } = error.response + if (data && typeof data.error === 'string') { + let msg = data.error || data.message + if (typeof msg === 'string') { + const m = msg.match(/^JsDefined\((.*)\)$/) + if (m) { + try { + const { message } = JSON.parse(m[1]) + if (typeof message === 'string') { + msg = message + } + } catch (e) { + logger.warn("can't parse server result", e) + } + } + if (msg && msg[0] !== '<') { + throw new LedgerAPIErrorWithMessage(msg) + } + } + } + const { status } = error.response + logger.log('Ledger API: HTTP status', status, 'data: ', error.response.data) + throw new LedgerAPIError(`LedgerAPIError ${status}`, { status }) + } else if (error.request) { + // The request was made but no response was received + // `error.request` is an instance of XMLHttpRequest in the browser and an instance of + // http.ClientRequest in node.js + throw new NetworkDown() + } + throw error + }) let implementation = (arg: Object) => { let promise