Radarr/frontend/src/Store/Middleware/createSentryMiddleware.js

94 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-11-23 07:04:42 +00:00
import _ from 'lodash';
import * as sentry from '@sentry/browser';
import parseUrl from 'Utilities/String/parseUrl';
function cleanseUrl(url) {
const properties = parseUrl(url);
return `${properties.pathname}${properties.search}`;
}
function cleanseData(data) {
const result = _.cloneDeep(data);
result.transaction = cleanseUrl(result.transaction);
if (result.exception) {
result.exception.values.forEach((exception) => {
const stacktrace = exception.stacktrace;
if (stacktrace) {
stacktrace.frames.forEach((frame) => {
frame.filename = cleanseUrl(frame.filename);
});
}
});
}
result.request.url = cleanseUrl(result.request.url);
return result;
}
function identity(stuff) {
return stuff;
}
function createMiddleware() {
return (store) => (next) => (action) => {
try {
// Adds a breadcrumb for reporting later (if necessary).
sentry.addBreadcrumb({
category: 'redux',
message: action.type
});
return next(action);
} catch (err) {
console.error(`[sentry] Reporting error to Sentry: ${err}`);
// Send the report including breadcrumbs.
sentry.captureException(err, {
extra: {
action: identity(action),
state: identity(store.getState())
}
});
}
};
}
export default function createSentryMiddleware() {
const {
2019-08-07 02:20:47 +00:00
analytics,
2018-11-23 07:04:42 +00:00
branch,
version,
release,
2019-08-07 02:20:47 +00:00
userHash,
2018-11-23 07:04:42 +00:00
isProduction
} = window.Radarr;
2019-08-07 02:20:47 +00:00
if (!analytics) {
2018-11-23 07:04:42 +00:00
return;
}
2019-08-07 02:20:47 +00:00
const dsn = isProduction ? 'https://cf0559cfe5c84ccfae3d907daef1b6bb@sentry.io/1523530' :
'https://df167d10dc51480b9e4f22e4e77c7315@sentry.io/1523531';
2018-11-23 07:04:42 +00:00
sentry.init({
dsn,
2019-08-07 02:20:47 +00:00
environment: branch,
2018-11-23 07:04:42 +00:00
release,
sendDefaultPii: true,
beforeSend: cleanseData
});
sentry.configureScope((scope) => {
2019-08-07 02:20:47 +00:00
scope.setUser({ username: userHash });
2018-11-23 07:04:42 +00:00
scope.setTag('version', version);
2019-08-07 02:20:47 +00:00
scope.setTag('production', isProduction);
2018-11-23 07:04:42 +00:00
});
return createMiddleware();
}