From bac0f094f3a9a96e120380f88743a06d317c060e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Fri, 22 Jun 2018 13:04:54 +0200 Subject: [PATCH] Add a logger.critical for raising exception to Sentry --- src/components/ThrowBlock.js | 2 +- src/logger.js | 16 ++++++++++++++++ src/renderer/init.js | 2 +- src/sentry/browser.js | 4 ++++ src/sentry/node.js | 4 ++++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/components/ThrowBlock.js b/src/components/ThrowBlock.js index 89b0d497..195ee000 100644 --- a/src/components/ThrowBlock.js +++ b/src/components/ThrowBlock.js @@ -17,7 +17,7 @@ class ThrowBlock extends PureComponent { } componentDidCatch(error: Error) { - logger.error(error) + logger.critical(error) this.setState({ error }) } diff --git a/src/logger.js b/src/logger.js index cc6cfc82..0036f0fc 100644 --- a/src/logger.js +++ b/src/logger.js @@ -134,15 +134,31 @@ export default { console.log(...args) addLog('log', ...args) }, + warn: (...args: any) => { console.warn(...args) addLog('warn', ...args) }, + error: (...args: any) => { console.error(...args) addLog('error', ...args) }, + critical: (error: Error) => { + addLog('critical', error) + console.error(error) + try { + if (typeof window !== 'undefined') { + require('sentry/browser').captureException(error) + } else { + require('sentry/node').captureException(error) + } + } catch (e) { + console.warn("Can't send to sentry", error, e) + } + }, + exportLogs: (): Array<{ type: string, date: Date, args: Array }> => logs.map(({ type, date, args }) => ({ type, diff --git a/src/renderer/init.js b/src/renderer/init.js index a91a2087..6d711bf3 100644 --- a/src/renderer/init.js +++ b/src/renderer/init.js @@ -99,6 +99,6 @@ function r(Comp) { init().catch(e => { // for now we make the app crash instead of pending forever. later we can render the error OR try to recover, but probably this is unrecoverable cases. - logger.error(e) + logger.critical(e) r() }) diff --git a/src/sentry/browser.js b/src/sentry/browser.js index ac5fc55c..8b4b60d9 100644 --- a/src/sentry/browser.js +++ b/src/sentry/browser.js @@ -7,3 +7,7 @@ import install from './install' export default (shouldSendCallback: () => boolean) => { install(Raven, shouldSendCallback, user().id) } + +export const captureException = (e: Error) => { + Raven.captureException(e) +} diff --git a/src/sentry/node.js b/src/sentry/node.js index a617487b..bf19a6e3 100644 --- a/src/sentry/node.js +++ b/src/sentry/node.js @@ -6,3 +6,7 @@ import install from './install' export default (shouldSendCallback: () => boolean, userId: string) => { install(Raven, shouldSendCallback, userId) } + +export const captureException = (e: Error) => { + Raven.captureException(e) +}