Last active
September 10, 2020 15:02
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow weak | |
import raven from 'raven-js'; | |
import config from 'config'; | |
const SENTRY_DSN = 'https://[email protected]/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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same question 🆙