Browse Source

Merge pull request #735 from gre/debug-network

Add DEBUG_NETWORK for logging http queries
master
Meriadec Pillet 7 years ago
committed by GitHub
parent
commit
147f12df34
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 45
      src/api/network.js
  2. 1
      src/config/constants.js
  3. 4
      src/internals/index.js
  4. 76
      src/logger.js

45
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 = <A>(p: Promise<A>): Promise<A> =>
const userFriendlyError = <A>(p: Promise<A>, { url, method, startTime }): Promise<A> =>
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 = <A>(p: Promise<A>): Promise<A> =>
} 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: *) => {

1
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

4
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) => {

76
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

Loading…
Cancel
Save