Skip to content

Instantly share code, notes, and snippets.

@unlabeled
Created November 3, 2017 12:30
Show Gist options
  • Save unlabeled/d351cc32b646733b1a6388e1d7a50fcd to your computer and use it in GitHub Desktop.
Save unlabeled/d351cc32b646733b1a6388e1d7a50fcd to your computer and use it in GitHub Desktop.
import Raven from "raven-js"
import { sentry, release } from "../../config"
import Api from "utils/api"
const immediately = false
const errors = []
/* eslint-disable */
const dev = Boolean(__DEV__)
/* eslint-enable */
/**
* Handle errors with sentry
*/
export const handleErrors = function() {
if (!dev && sentry) {
Raven.config(sentry, { release }).install()
}
}
/**
* Set user details for sentry
*/
export const setUserContext = function(user) {
Raven.setUserContext(user)
}
/**
* Send errors
*/
export const sendError = function(error, errorInfo) {
if (!dev && sentry) {
errors.push({ error, extra: errorInfo })
sendAfterAllApiRequests()
}
}
/**
* Delayed sending
* TODO: Remove when we will have localStorage for users
*/
function sendAfterAllApiRequests() {
if (errors.length) {
if (!immediately && Api.pending.size !== 0) {
setTimeout(sendAfterAllApiRequests, 100)
} else {
const error = errors.shift()
sendImmediately(error)
sendAfterAllApiRequests()
}
}
}
/**
* Send error immediately
*/
function sendImmediately({ error, extra }) {
Raven.captureException(error, { extra })
}
/**
* Delayed sending
* TODO: Remove when we will have localStorage for users
*/
function sendAfterAllApiRequests() {
if (errors.length) {
if (!immediately && Api.pending.size !== 0) {
setTimeout(sendAfterAllApiRequests, 100)
} else {
const error = errors.shift()
sendImmediately(error)
sendAfterAllApiRequests()
}
}
}
/**
* Send error immediately
*/
function sendImmediately({ error, extra }) {
Raven.captureException(error, { extra })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment