Skip to content

Instantly share code, notes, and snippets.

@rgoldfinger
Created September 14, 2017 14:27
Show Gist options
  • Save rgoldfinger/00481ee74e835042c65a5a6d81613e63 to your computer and use it in GitHub Desktop.
Save rgoldfinger/00481ee74e835042c65a5a6d81613e63 to your computer and use it in GitHub Desktop.
/* @flow */
import idx from 'idx';
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(payload) {
const message =
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);
if (message && typeof message === 'string') {
const shouldAddDescription = typeof description === 'string' && description !== message;
payload.fingerprint = message + (shouldAddDescription ? description : '');
} else if (description && typeof description === 'string') {
payload.fingerprint = description;
}
},
checkIgnore(isUncaught, args, payload) {
try {
const messagesToIgnore = [
'unhandled rejection was null or undefined',
'script error',
'[object Object]',
'GraphQL error: 503',
'GraphQL error: 401',
'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.', // error with chrome on ios. Verified that it works correctly.
'event is not defined',
'Failed to load because no supported source was found.',
];
if (payload.fingerprint) {
console.log(
messagesToIgnore.some(message => new RegExp(message, 'gi').test(payload.fingerprint))
);
return messagesToIgnore.some(message =>
new RegExp(message, 'gi').test(payload.fingerprint)
);
}
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;
}
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment