Skip to content

Instantly share code, notes, and snippets.

@rgoldfinger
Created October 4, 2017 17:01
Show Gist options
  • Save rgoldfinger/edfc5056b035a3fcb96f85e885733ca8 to your computer and use it in GitHub Desktop.
Save rgoldfinger/edfc5056b035a3fcb96f85e885733ca8 to your computer and use it in GitHub Desktop.
/* @flow */
import idx from 'idx';
const regexsToIgnore = [
/Request blocked by client, probably adblock.*Branch/gi, // Branch init
/Request timed out.*Branch/gi, // Branch init
/GoogleAPI.*popup_closed_by_user/gi, // google auth
/GoogleAPI.*access_denied/gi, // google auth
/unhandled rejection was null or undefined/gi,
/script error/gi,
/object Object/gi,
/GraphQL error: 503/gi,
/GraphQL error: Unauthorized/gi,
/GraphQL error: 401/gi,
/Network error: Failed to fetch/gi,
// prettier-ignore
/\$ is not defined/gi, // eslint-disable-line
/Blocked a frame with origin "https:\/\/www.remind.com" from accessing a frame with origin "https:\/\/accounts.google.com". Protocols, domains, and ports must match./gi, // error with chrome on ios. Verified that it works correctly.
/event is not defined/gi,
/Failed to load because no supported source was found./gi,
/maxItems has been hit. Ignoring errors until reset./gi, // When rollbar's max items per broswer session have been hit
];
export function checkIgnore(isUncaught: boolean, args: Array<*>, payload: Object) {
try {
if (payload.fingerprint) {
return regexsToIgnore.some(pattern => payload.fingerprint.search(pattern) !== -1);
}
return false;
} catch (e) {
// if we did this wrong, lets log it so we can track why, and send the error anyway.
console.error(e);
return false;
}
}
export function transform(payload: Object) {
const message =
idx(payload, _ => _.body.message.body) ||
idx(payload, _ => _.body.message) ||
idx(payload, _ => _.body.trace.exception.message) ||
idx(payload, _ => _.body.trace.extra.extraArgs[0].message) ||
idx(payload, _ => _.body.trace.extra.extraArgs[1].message) ||
idx(payload, _ => _.body.trace.extra.extraArgs[0]) ||
idx(payload, _ => _.body.trace.extra.extraArgs[1]);
const description =
idx(payload, _ => _.body.trace.exception.description) ||
idx(payload, _ => _.body.message.extra.error);
if (typeof message === 'string' && message !== '') {
const shouldAddDescription = typeof description === 'string' && description !== message;
payload.fingerprint = message + (shouldAddDescription ? ` ${description || ''}` : '');
} else if (description && typeof description === 'string') {
payload.fingerprint = description;
}
}
if (typeof window !== 'undefined' && window.Rollbar) {
// TODO some way to disable console output. This seems to require a PR to rollbar's lib.
const { Rollbar } = window;
const nativeConsoleError = console.error;
// $FlowFixMe
console.error = (...args) => {
// If there's an error passed to the console, we want to use thar directly to preserve the stacktrace
const hasError = args.some(e => e instanceof Error);
if (hasError) {
Rollbar.error(...args);
} else {
// otherwise generate a stack trace
try {
throw Error(args[0]);
} catch (e) {
Rollbar.error(e, ...args.slice(1));
}
}
return nativeConsoleError.apply(console, args);
};
Rollbar.global({
maxItems: 10,
});
Rollbar.configure({
transform,
checkIgnore,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment