From 875d47a2ea900d950c62a04c2b2e664bb1c70976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Wed, 27 Jun 2018 19:34:58 +0200 Subject: [PATCH] Add DEBUG_NETWORK for logging http queries the old DEBUG_NETWORK is now in experimental flags because i can't make it work --- src/api/network.js | 45 ++++++++++++++++++------ src/config/constants.js | 1 + src/internals/index.js | 4 +-- src/logger.js | 76 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 108 insertions(+), 18 deletions(-) diff --git a/src/api/network.js b/src/api/network.js index f4311539..64d510e1 100644 --- a/src/api/network.js +++ b/src/api/network.js @@ -9,12 +9,13 @@ export const LedgerAPIErrorWithMessage = createCustomErrorClass('LedgerAPIErrorW export const LedgerAPIError = createCustomErrorClass('LedgerAPIError') export const NetworkDown = createCustomErrorClass('NetworkDown') -const userFriendlyError = (p: Promise): Promise => +const userFriendlyError = (p: Promise, { url, method, startTime }): Promise => p.catch(error => { + let errorToThrow 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 + const { data, status } = error.response if (data && typeof data.error === 'string') { let msg = data.error || data.message if (typeof msg === 'string') { @@ -32,19 +33,28 @@ const userFriendlyError = (p: Promise): Promise => } catch (e) { logger.warn("can't parse server result", e) } - if (msg && msg[0] !== '<') { - throw new LedgerAPIErrorWithMessage(msg) + errorToThrow = new LedgerAPIErrorWithMessage(msg) } } } - const { status } = error.response - logger.log('Ledger API: HTTP status', status, 'data: ', error.response.data) - throw new LedgerAPIError(`LedgerAPIError ${status}`, { status }) + if (!errorToThrow) { + errorToThrow = new LedgerAPIError(`LedgerAPIError ${status}`, { status }) + } + logger.networkError({ + status, + url, + method, + error: errorToThrow.message, + responseTime: Date.now() - startTime, + }) + throw errorToThrow } 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 + logger.networkDown({ + url, + method, + responseTime: Date.now() - startTime, + }) throw new NetworkDown() } throw error @@ -62,7 +72,20 @@ let implementation = (arg: Object) => { } else { promise = axios(arg) } - return userFriendlyError(promise) + const meta = { + url: arg.url, + method: arg.method, + startTime: Date.now(), + } + logger.network(meta) + promise.then(response => { + logger.networkSucceed({ + ...meta, + status: response.status, + responseTime: Date.now() - meta.startTime, + }) + }) + return userFriendlyError(promise, meta) } export const setImplementation = (impl: *) => { diff --git a/src/config/constants.js b/src/config/constants.js index efa893bd..f873cdcc 100644 --- a/src/config/constants.js +++ b/src/config/constants.js @@ -78,6 +78,7 @@ export const HIGHLIGHT_I18N = boolFromEnv('HIGHLIGHT_I18N') export const DISABLE_ACTIVITY_INDICATORS = boolFromEnv('DISABLE_ACTIVITY_INDICATORS') export const EXPERIMENTAL_CENTER_MODAL = boolFromEnv('EXPERIMENTAL_CENTER_MODAL') export const EXPERIMENTAL_FIRMWARE_UPDATE = boolFromEnv('EXPERIMENTAL_FIRMWARE_UPDATE') +export const EXPERIMENTAL_HTTP_ON_RENDERER = boolFromEnv('EXPERIMENTAL_HTTP_ON_RENDERER') // Other constants diff --git a/src/internals/index.js b/src/internals/index.js index 272a9153..0db57625 100644 --- a/src/internals/index.js +++ b/src/internals/index.js @@ -4,7 +4,7 @@ import logger from 'logger' import uuid from 'uuid/v4' import { setImplementation } from 'api/network' import sentry from 'sentry/node' -import { DEBUG_NETWORK } from 'config/constants' +import { EXPERIMENTAL_HTTP_ON_RENDERER } from 'config/constants' import { serializeError } from 'helpers/errors' require('../env') @@ -17,7 +17,7 @@ let sentryEnabled = process.env.INITIAL_SENTRY_ENABLED || false sentry(() => sentryEnabled, process.env.SENTRY_USER_ID) -if (DEBUG_NETWORK) { +if (EXPERIMENTAL_HTTP_ON_RENDERER) { setImplementation(networkArg => { const id = uuid() return new Promise((resolve, reject) => { diff --git a/src/logger.js b/src/logger.js index 757835c1..f06c8fed 100644 --- a/src/logger.js +++ b/src/logger.js @@ -10,6 +10,7 @@ */ import { + DEBUG_NETWORK, DEBUG_COMMANDS, DEBUG_DB, DEBUG_ACTION, @@ -62,6 +63,7 @@ const logRedux = !__DEV__ || DEBUG_ACTION const logTabkey = !__DEV__ || DEBUG_TAB_KEY const logLibcore = !__DEV__ || DEBUG_LIBCORE const logWS = !__DEV__ || DEBUG_WS +const logNetwork = !__DEV__ || DEBUG_NETWORK export default { onCmd: (type: string, id: string, spentTime: number, data?: any) => { @@ -86,7 +88,7 @@ export default { }, onDB: (way: 'read' | 'write' | 'clear', name: string, obj: ?Object) => { - const msg = `📁 ${way} ${name}:` + const msg = `📁 ${way} ${name}:` if (logDb) { console.log(msg, obj) } @@ -97,9 +99,9 @@ export default { onReduxAction: (action: Object) => { if (logRedux) { - console.log(`⚛️ ${action.type}`, action) + console.log(`⚛️ ${action.type}`, action) } - addLog('action', `⚛️ ${action.type}`, action) + addLog('action', `⚛️ ${action.type}`, action) }, // tracks keyboard events @@ -123,9 +125,73 @@ export default { libcore: (level: string, msg: string) => { if (logLibcore) { - console.log(`🛠 ${level}: ${msg}`) + console.log(`🛠 ${level}: ${msg}`) } - addLog('action', `🛠 ${level}: ${msg}`) + addLog('action', `🛠 ${level}: ${msg}`) + }, + + network: ({ method, url }: { method: string, url: string }) => { + const log = `➡📡 ${method} ${url}` + if (logNetwork) { + console.log(log) + } + addLog('network', log) + }, + + networkSucceed: ({ + method, + url, + status, + responseTime, + }: { + method: string, + url: string, + status: number, + responseTime: number, + }) => { + const log = `✔📡 HTTP ${status} ${method} ${url} – finished in ${responseTime.toFixed(0)}ms` + if (logNetwork) { + console.log(log) + } + addLog('network-response', log) + }, + + networkError: ({ + method, + url, + status, + error, + responseTime, + }: { + method: string, + url: string, + status: number, + error: string, + responseTime: number, + }) => { + const log = `✖📡 HTTP ${status} ${method} ${url} – ${error} – failed after ${responseTime.toFixed( + 0, + )}ms` + if (logNetwork) { + console.log(log) + } + addLog('network-error', log) + }, + + networkDown: ({ + method, + url, + responseTime, + }: { + method: string, + url: string, + responseTime: number, + }) => { + const log = `✖📡 NETWORK DOWN – ${method} ${url} – after ${responseTime.toFixed(0)}ms` + if (logNetwork) { + console.log(log) + } + addLog('network-down', log) }, // General functions in case the hooks don't apply