From f64e3e0204381f2667c6baa4cc78c6c844fc0f21 Mon Sep 17 00:00:00 2001 From: Sonny Date: Wed, 2 May 2018 01:10:44 -0700 Subject: [PATCH] Adding ability to send error object to notify() (#2955) --- app/config/init.js | 4 +--- app/notify.js | 6 +++++- app/plugins.js | 20 ++++++++++++-------- lib/actions/ui.js | 4 +--- lib/utils/notify.js | 6 +++++- lib/utils/plugins.js | 34 +++++++++++++++------------------- 6 files changed, 39 insertions(+), 35 deletions(-) diff --git a/app/config/init.js b/app/config/init.js index e741f760..80373b92 100644 --- a/app/config/init.js +++ b/app/config/init.js @@ -15,9 +15,7 @@ const _syntaxValidation = function(cfg) { try { return new vm.Script(cfg, {filename: '.hyper.js', displayErrors: true}); } catch (err) { - notify('Error loading config:', `${err.name}, see DevTools for more info`); - //eslint-disable-next-line no-console - console.error('Error loading config:', err); + notify('Error loading config:', `${err.name}, see DevTools for more info`, {error: err}); } }; diff --git a/app/notify.js b/app/notify.js index 1d00428e..266ceefa 100644 --- a/app/notify.js +++ b/app/notify.js @@ -27,9 +27,13 @@ app.on('ready', () => { }); }); -function notify(title, body) { +function notify(title, body, details = {}) { //eslint-disable-next-line no-console console.log(`[Notification] ${title}: ${body}`); + if (details.error) { + //eslint-disable-next-line no-console + console.error(details.error); + } if (win) { win.webContents.send('notification', {title, body}); } else { diff --git a/app/plugins.js b/app/plugins.js index 0be9d458..efb085f9 100644 --- a/app/plugins.js +++ b/app/plugins.js @@ -66,7 +66,7 @@ function updatePlugins({force = false} = {}) { if (err) { //eslint-disable-next-line no-console - notify('Error updating plugins.', err); + notify('Error updating plugins.', err, {error: err}); } else { // flag successful plugin update cache.set('hyper.plugins', id_); @@ -257,9 +257,7 @@ function requirePlugins() { //eslint-disable-next-line no-console console.warn(`Plugin "${basename(path_)}" not found: ${path_}`); } else { - //eslint-disable-next-line no-console - console.error(err); - notify('Plugin error!', `Plugin "${basename(path_)}" failed to load (${err.message})`); + notify('Plugin error!', `Plugin "${basename(path_)}" failed to load (${err.message})`, {error: err}); } } }; @@ -276,7 +274,9 @@ exports.onApp = app_ => { try { plugin.onApp(app_); } catch (e) { - notify('Plugin error!', `"${plugin._name}" has encountered an error. Check Developer Tools for details.`); + notify('Plugin error!', `"${plugin._name}" has encountered an error. Check Developer Tools for details.`, { + error: e + }); } } }); @@ -288,7 +288,9 @@ exports.onWindow = win => { try { plugin.onWindow(win); } catch (e) { - notify('Plugin error!', `"${plugin._name}" has encountered an error. Check Developer Tools for details.`); + notify('Plugin error!', `"${plugin._name}" has encountered an error. Check Developer Tools for details.`, { + error: e + }); } } }); @@ -304,7 +306,7 @@ function decorateObject(base, key) { try { res = plugin[key](decorated); } catch (e) { - notify('Plugin error!', `"${plugin._name}" when decorating ${key}`); + notify('Plugin error!', `"${plugin._name}" when decorating ${key}`, {error: e}); return; } if (res && typeof res === 'object') { @@ -330,7 +332,9 @@ exports.getDeprecatedConfig = () => { try { configTmp = plugin.decorateConfig(JSON.parse(JSON.stringify(baseConfig))); } catch (e) { - notify('Plugin error!', `"${plugin._name}" has encountered an error. Check Developer Tools for details.`); + notify('Plugin error!', `"${plugin._name}" has encountered an error. Check Developer Tools for details.`, { + error: e + }); return; } const pluginCSSDeprecated = config.getDeprecatedCSS(configTmp); diff --git a/lib/actions/ui.js b/lib/actions/ui.js index 67f6a4e4..ec5b24d6 100644 --- a/lib/actions/ui.js +++ b/lib/actions/ui.js @@ -258,9 +258,7 @@ export function openFile(path) { effect() { stat(path, (err, stats) => { if (err) { - //eslint-disable-next-line no-console - console.error(err.stack); - notify('Unable to open path', `"${path}" doesn't exist.`); + notify('Unable to open path', `"${path}" doesn't exist.`, {error: err}); } else { let command = escapeShellCmd(path).replace(/ /g, '\\ '); if (stats.isDirectory()) { diff --git a/lib/utils/notify.js b/lib/utils/notify.js index 4fcf9109..fedeacf9 100644 --- a/lib/utils/notify.js +++ b/lib/utils/notify.js @@ -1,7 +1,11 @@ /* global Notification */ /* eslint no-new:0 */ -export default function notify(title, body) { +export default function notify(title, body, details = {}) { //eslint-disable-next-line no-console console.log(`[Notification] ${title}: ${body}`); + if (details.error) { + //eslint-disable-next-line no-console + console.error(details.error); + } new Notification(title, {body}); } diff --git a/lib/utils/plugins.js b/lib/utils/plugins.js index 4c28ebeb..71700f0e 100644 --- a/lib/utils/plugins.js +++ b/lib/utils/plugins.js @@ -137,11 +137,10 @@ const loadModules = () => { try { mod = window.require(path); } catch (err) { - //eslint-disable-next-line no-console - console.error(err.stack); notify( 'Plugin load error', - `"${pluginName}" failed to load in the renderer process. Check Developer Tools for details.` + `"${pluginName}" failed to load in the renderer process. Check Developer Tools for details.`, + {error: err} ); return undefined; } @@ -276,9 +275,9 @@ function getProps(name, props, ...fnArgs) { try { ret_ = fn(...fnArgs, props_); } catch (err) { - //eslint-disable-next-line no-console - console.error(err.stack); - notify('Plugin error', `${fn._pluginName}: Error occurred in \`${name}\`. Check Developer Tools for details.`); + notify('Plugin error', `${fn._pluginName}: Error occurred in \`${name}\`. Check Developer Tools for details.`, { + error: err + }); return; } @@ -323,11 +322,10 @@ export function connect(stateFn, dispatchFn, c, d = {}) { try { ret_ = fn(state, ret); } catch (err) { - //eslint-disable-next-line no-console - console.error(err.stack); notify( 'Plugin error', - `${fn._pluginName}: Error occurred in \`map${name}State\`. Check Developer Tools for details.` + `${fn._pluginName}: Error occurred in \`map${name}State\`. Check Developer Tools for details.`, + {error: err} ); return; } @@ -349,11 +347,10 @@ export function connect(stateFn, dispatchFn, c, d = {}) { try { ret_ = fn(dispatch, ret); } catch (err) { - //eslint-disable-next-line no-console - console.error(err.stack); notify( 'Plugin error', - `${fn._pluginName}: Error occurred in \`map${name}Dispatch\`. Check Developer Tools for details.` + `${fn._pluginName}: Error occurred in \`map${name}Dispatch\`. Check Developer Tools for details.`, + {error: err} ); return; } @@ -387,9 +384,9 @@ function decorateReducer(name, fn) { try { state__ = pluginReducer(state_, action); } catch (err) { - //eslint-disable-next-line no-console - console.error(err.stack); - notify('Plugin error', `${fn._pluginName}: Error occurred in \`${name}\`. Check Developer Tools for details.`); + notify('Plugin error', `${fn._pluginName}: Error occurred in \`${name}\`. Check Developer Tools for details.`, { + error: err + }); return; } @@ -436,7 +433,7 @@ function exposeDecorated(Component_) { try { this.props.onDecorated(decorated_); } catch (e) { - notify('Plugin error', `Error occurred. Check Developer Tools for details`); + notify('Plugin error', `Error occurred. Check Developer Tools for details`, {error: e}); } } } @@ -462,11 +459,10 @@ function getDecorated(parent, name) { class__ = fn(class_, {React, PureComponent, Notification, notify}); class__.displayName = `${fn._pluginName}(${name})`; } catch (err) { - //eslint-disable-next-line no-console - console.error(err.stack); notify( 'Plugin error', - `${fn._pluginName}: Error occurred in \`${method}\`. Check Developer Tools for details` + `${fn._pluginName}: Error occurred in \`${method}\`. Check Developer Tools for details`, + {error: err} ); return; }