From 361b3719d532ec3ae8dd1ca492acb0ca5c9954fc Mon Sep 17 00:00:00 2001 From: Juan Antonio Gomez Benito Date: Mon, 16 Oct 2017 11:25:14 +0200 Subject: [PATCH 1/2] [feat] a system to skip errors by type --- addon/utils/bugsnag-skip.js | 19 +++++++++++++++++++ addon/utils/errors.js | 28 +++++++++++++++++++++++++++- app/instance-initializers/bugsnag.js | 24 ++++++++++++++---------- app/utils/bugsnag-skip.js | 7 +++++++ tests/dummy/config/environment.js | 7 ++++++- 5 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 addon/utils/bugsnag-skip.js create mode 100644 app/utils/bugsnag-skip.js diff --git a/addon/utils/bugsnag-skip.js b/addon/utils/bugsnag-skip.js new file mode 100644 index 0000000..1dc6e8b --- /dev/null +++ b/addon/utils/bugsnag-skip.js @@ -0,0 +1,19 @@ +/** + * Default functions to determine if an error must be skipped (true) or included (false). + * + * All error types that can be skipped are listed in config/environment.js + */ +export const errorIs = { + + /** + * Returns if is a plain error. + * + * @method plain + * @param {Mixed} error + * @return {Boolean} + */ + plain(error) { + return !(error instanceof Error); + } + +}; diff --git a/addon/utils/errors.js b/addon/utils/errors.js index c7de251..46a0c47 100644 --- a/addon/utils/errors.js +++ b/addon/utils/errors.js @@ -1,7 +1,12 @@ import Ember from 'ember'; import XHRError from './xhr-error'; +import { errorIs } from './bugsnag-skip'; -const { get, isNone } = Ember; +const { + debug, + get, + isNone +} = Ember; export function getContext(router) { const infos = router.currentState.routerJsState.handlerInfos; @@ -58,3 +63,24 @@ export function getError(error) { return new Error(error); } + +/** + * Skips the error if any condition is satisfied. + * + * @param {Mixed} error Error caught + * @return {Boolean} True to skip the error; False to include it. + */ +export function skipError(error, skipErrorTypes = {}) { + let result = false; + + Object.keys(skipErrorTypes).forEach((name) => { + if ((skipErrorTypes[name] === true) && (typeof errorIs[name] === 'function') && (errorIs[name](error))) { + debug(`The error is skipped by fulfilling the condition '${name}'`); + result = true; + + return; + } + }); + + return result; +} diff --git a/app/instance-initializers/bugsnag.js b/app/instance-initializers/bugsnag.js index 831b8f8..4f620bd 100644 --- a/app/instance-initializers/bugsnag.js +++ b/app/instance-initializers/bugsnag.js @@ -1,7 +1,8 @@ import Ember from 'ember'; import config from '../config/environment'; -import { getContext, generateError, getError } from 'ember-cli-bugsnag/utils/errors'; +import { getContext, generateError, getError, skipError } from 'ember-cli-bugsnag/utils/errors'; import { getMetaData } from '../utils/bugsnag'; +import { errorIs } from '../utils/bugsnag-skip'; import Bugsnag from 'bugsnag'; export default { @@ -20,23 +21,26 @@ export default { let router = owner.lookup('router:main'); Ember.onerror = function(error) { - const plain = !(error instanceof Error); + const skip = skipError(error, config.bugsnag.skipErrorTypes); + const plain = errorIs.plain(error); if (plain) { error = getError(error); } - if (isBugsnagActive) { - const metaData = getMetaData(error, owner) || {}; + if (skip !== true) { + if (isBugsnagActive) { + const metaData = getMetaData(error, owner) || {}; - // Group all plain errors by message. - if (plain) { - metaData.groupingHash = error ? error.message : 'No message found on error object'; + // Group all plain errors by message. + if (plain) { + metaData.groupingHash = error ? error.message : 'No message found on error object'; + } + Bugsnag.context = getContext(router); + Bugsnag.notifyException(error, null, metaData); } - Bugsnag.context = getContext(router); - Bugsnag.notifyException(error, null, metaData); + console.error(error.message, error.stack); } - console.error(error.message, error.stack); }; Ember.Logger.error = function(message, cause, stack) { diff --git a/app/utils/bugsnag-skip.js b/app/utils/bugsnag-skip.js new file mode 100644 index 0000000..f3df709 --- /dev/null +++ b/app/utils/bugsnag-skip.js @@ -0,0 +1,7 @@ +/** + * Functions to determine if an error must be skipped (true) or included (false). + * + * Default functions are defined in `ember-cli-bugsnag/addon/utils-bugsnag-skip.js`. + * All error types that can be skipped must be listed in the config/environment.js of the app. + */ +export const errorIs = {}; diff --git a/tests/dummy/config/environment.js b/tests/dummy/config/environment.js index 406292b..852649f 100644 --- a/tests/dummy/config/environment.js +++ b/tests/dummy/config/environment.js @@ -20,7 +20,12 @@ module.exports = function(environment) { bugsnag: { apiKey: "09e2825dc7b16fdbf7b207cf5b780064", - notifyReleaseStages: ['production', 'development'] + notifyReleaseStages: ['production', 'development'], + skipErrorTypes: { + closeConfirmModal: true, + serverError: false, + plain: false + } } }; From c3d10c146426e246de89bf99f945814a0eb2a70d Mon Sep 17 00:00:00 2001 From: Juan Antonio Gomez Benito Date: Tue, 17 Oct 2017 16:17:04 +0200 Subject: [PATCH 2/2] [fix] Remove non-default types --- tests/dummy/config/environment.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/dummy/config/environment.js b/tests/dummy/config/environment.js index 852649f..499c03b 100644 --- a/tests/dummy/config/environment.js +++ b/tests/dummy/config/environment.js @@ -22,8 +22,6 @@ module.exports = function(environment) { apiKey: "09e2825dc7b16fdbf7b207cf5b780064", notifyReleaseStages: ['production', 'development'], skipErrorTypes: { - closeConfirmModal: true, - serverError: false, plain: false } }