Created
January 10, 2017 16:19
-
-
Save husek/603dcc066cdb165d138b34fb2cdf90a4 to your computer and use it in GitHub Desktop.
Raven JS (Sentry JS Client) Redux Middleware example
This file contains 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
import is from 'is-type-of'; | |
import { isDOMAvailable } from 'utils'; | |
if (typeof Raven !== 'undefined') { | |
const ravenConfig = { | |
// config from https://docs.getsentry.com/hosted/clients/javascript/tips/ | |
ignoreErrors: [ | |
// Random plugins/extensions | |
'top.GLOBALS', | |
// See: http://blog.errorception.com/2012/03/tale-of-unfindable-js-error. html | |
'originalCreateNotification', | |
'canvas.contentDocument', | |
'MyApp_RemoveAllHighlights', | |
'http://tt.epicplay.com', | |
'Can\'t find variable: ZiteReader', | |
'jigsaw is not defined', | |
'ComboSearch is not defined', | |
'http://loading.retry.widdit.com/', | |
'atomicFindClose', | |
// Facebook borked | |
'fb_xd_fragment', | |
// ISP "optimizing" proxy - `Cache-Control: no-transform` seems to | |
// reduce this. (thanks @acdha) | |
// See http://stackoverflow.com/questions/4113268 | |
'bmi_SafeAddOnload', | |
'EBCallBackMessageReceived', | |
// See http://toolbar.conduit.com/Developer/HtmlAndGadget/Methods/JSInjection.aspx | |
'conduitPage' | |
], | |
ignoreUrls: [ | |
// Localhost, duh | |
/localhost/, | |
// Facebook flakiness | |
/graph\.facebook\.com/i, | |
// Facebook blocked | |
/connect\.facebook\.net\/en_US\/all\.js/i, | |
// Woopra flakiness | |
/eatdifferent\.com\.woopra-ns\.com/i, | |
/static\.woopra\.com\/js\/woopra\.js/i, | |
// Chrome extensions | |
/extensions\//i, | |
/^chrome:\/\//i, | |
// Other plugins | |
/127\.0\.0\.1:4001\/isrunning/i, // Cacaoweb | |
/webappstoolbarba\.texthelp\.com\//i, | |
/metrics\.itunes\.apple\.com\.edgesuite\.net\//i | |
] | |
}; | |
Raven.config(`https://${__SENTRY__.key}@app.getsentry.com/${__SENTRY__.id}`, ravenConfig).install(); | |
} else { | |
if (isDOMAvailable()) { | |
window.Raven = { | |
captureException(err, extraData) { | |
console.warn('Raven not set!'); | |
console.error(err); | |
console.error(extraData); | |
}, | |
captureBreadcrumb(data) { | |
console.warn('Raven not set!'); | |
console.log(data); | |
} | |
}; | |
} | |
} | |
function captureException(err, action, appState) { | |
if (typeof Raven !== 'undefined') { | |
Raven.captureException(err, { | |
extra: { | |
action: action, | |
data: { | |
app: appState | |
} | |
} | |
}); | |
} | |
} | |
export const ravenMiddleware = store => next => action => { | |
try { | |
if (typeof Raven !== 'undefined') { | |
Raven.captureBreadcrumb({ | |
data: { redux: action.type || 'No action.type defined' } | |
}); | |
} | |
// accordingly to the Redux FSA | |
// https://github.com/acdlite/flux-standard-action | |
if (action.error && is.error(action.payload)) { | |
captureException(action.payload, action.type, store.getState().app); | |
} | |
} catch (err) { | |
captureException(err, action, store.getState().app); | |
} | |
return next(action); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment