Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

[feat] a system to skip errors by type #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions addon/utils/bugsnag-skip.js
Original file line number Diff line number Diff line change
@@ -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);
}

};
28 changes: 27 additions & 1 deletion addon/utils/errors.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
24 changes: 14 additions & 10 deletions app/instance-initializers/bugsnag.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 && !plain) {
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) {
Expand Down
7 changes: 7 additions & 0 deletions app/utils/bugsnag-skip.js
Original file line number Diff line number Diff line change
@@ -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 = {};
5 changes: 4 additions & 1 deletion tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ module.exports = function(environment) {

bugsnag: {
apiKey: "09e2825dc7b16fdbf7b207cf5b780064",
notifyReleaseStages: ['production', 'development']
notifyReleaseStages: ['production', 'development'],
skipErrorTypes: {
plain: false
}
}
};

Expand Down