Skip to content

Instantly share code, notes, and snippets.

@oliviertassinari
Last active September 10, 2020 15:02
Show Gist options
  • Save oliviertassinari/73389727fe58373eef7b63d2d2c5ce5d to your computer and use it in GitHub Desktop.
Save oliviertassinari/73389727fe58373eef7b63d2d2c5ce5d to your computer and use it in GitHub Desktop.
Track offline errors with sentry.io and raven-js. https://github.com/getsentry/raven-js/issues/279
// @flow weak
import raven from 'raven-js';
import config from 'config';
const SENTRY_DSN = 'https://XXXX@app.getsentry.com/YYYY';
function sendQueue() {
const sentryOffline = JSON.parse(window.localStorage.sentryOffline);
if (sentryOffline.length > 0) {
raven._send(sentryOffline[0]);
}
}
const crashReporter = {
init: () => {
if (!window.localStorage.sentryOffline) {
window.localStorage.sentryOffline = '[]';
}
raven.config(SENTRY_DSN, {});
raven.install();
document.addEventListener('ravenFailure', ({data}) => {
// Only store it once.
if (!data.extra.retry) {
// Mutation with side effect.
data.extra.retry = true;
const sentryOffline = JSON.parse(window.localStorage.sentryOffline);
// We can't store too much data
if (sentryOffline.length < 10) {
sentryOffline.push(data); // We use a FIFO.
window.localStorage.sentryOffline = JSON.stringify(sentryOffline);
}
}
});
document.addEventListener('ravenSuccess', ({data}) => {
if (data.extra.retry === true) {
const sentryOffline = JSON.parse(window.localStorage.sentryOffline);
sentryOffline.shift(); // We use a FIFO.
window.localStorage.sentryOffline = JSON.stringify(sentryOffline);
}
// The last push succeded, let's try to clear the queue.
sendQueue();
});
// First load, let's try to clear the queue.
sendQueue();
},
};
export default crashReporter;
@alainjlavoie
Copy link

Raven.js doesn't have a ._send() method. Were you using a different branch of Raven.js?
Thx

@Akash91
Copy link

Akash91 commented Jul 16, 2018

Same question 🆙

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment