Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for logging errors to Sentry #327

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/state/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import { apiMiddleware } from 'redux-api-middleware';

import createRecurringReservations from './createRecurringReservations';
import sideEffects from './sideEffects';
import createSentryMiddleware from './sentry';

const isDevelopment = process.env.NODE_ENV !== 'production';
const storeEnhancers = [applyMiddleware(
const middlewares = [
apiMiddleware,
sideEffects,
createRecurringReservations,
)];
];
const sentryMiddleware = createSentryMiddleware();
if (sentryMiddleware) {
middlewares.push(sentryMiddleware);
}

const storeEnhancers = [applyMiddleware(...middlewares)];

const isDevelopment = process.env.NODE_ENV !== 'production';
if (isDevelopment) {
const createLogger = require('redux-logger'); // eslint-disable-line global-require

Expand Down
35 changes: 35 additions & 0 deletions app/state/middleware/sentry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Sentry from '@sentry/browser';
import createSentryMiddleware from 'redux-sentry-middleware';


if (process.env.SENTRY_DSN) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
});
}

function createMiddleware() {
if (!process.env.SENTRY_DSN) {
return null;
}
return createSentryMiddleware(Sentry, {
getUserContext: (state) => {
const user = state.auth.user;
const email = user.emails ? user.emails[0].value : '';

return {
email,
name: `${user.firstName} ${user.lastName}`,
id: user.id,
};
},
stateTransformer: (state) => {
const out = { ...state };
delete out.auth;
delete out.data;
return out;
},
});
}

export default createMiddleware;
6 changes: 6 additions & 0 deletions app/state/reducers/reservationSuccessModalReducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import immutable from 'seamless-immutable';
import { actionTypes as formActions } from 'redux-form';
import * as Sentry from '@sentry/browser';

import apiActionTypes from 'api/actionTypes';

Expand Down Expand Up @@ -29,6 +30,11 @@ function reservationSuccessModalReducer(state = initialState, action) {
});
}
case apiActionTypes.RESERVATION_POST_ERROR: {
const status = action.payload.status;
if (status >= 500 || status === 401 || status === 403) {
Sentry.captureMessage('Reservation POST failed');
}

const reservation = action.meta.reservation;
if (!reservation) return state;
const failReason = parseError(action.payload);
Expand Down
4 changes: 4 additions & 0 deletions config/webpack.development.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const path = require('path');

const dotenv = require('dotenv');
const webpack = require('webpack');
const merge = require('webpack-merge');

const common = require('./webpack.common');

dotenv.config({ path: path.resolve(__dirname, '../.env') });

module.exports = merge(common, {
entry: [
'babel-polyfill',
Expand Down Expand Up @@ -49,6 +52,7 @@ module.exports = merge(common, {
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development'),
'process.env.SENTRY_DSN': JSON.stringify(process.env.SENTRY_DSN),
SETTINGS: {
API_URL: JSON.stringify(process.env.API_URL || 'https://api.hel.fi/virkarespa/v1/'),
REPORT_URL: JSON.stringify(
Expand Down
1 change: 1 addition & 0 deletions config/webpack.production.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = merge(common, {
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
'process.env.SENTRY_DSN': JSON.stringify(process.env.SENTRY_DSN),
SETTINGS: {
API_URL: JSON.stringify(process.env.API_URL || 'https://api.hel.fi/virkarespa/v1/'),
REPORT_URL: JSON.stringify(
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"npm": "3.10.3"
},
"dependencies": {
"@sentry/browser": "5.2.1",
"autoprefixer": "6.5.3",
"babel-core": "6.18.2",
"babel-eslint": "7.1.0",
Expand Down Expand Up @@ -44,7 +45,7 @@
"moment-range": "2.2.0",
"moment-timezone": "0.5.13",
"morgan": "1.9.0",
"node-sass": "4.5.2",
"node-sass": "^4.5",
"normalizr": "2.2.1",
"passport": "0.3.2",
"passport-helsinki": "git://github.com/City-of-Helsinki/passport-helsinki.git",
Expand All @@ -71,6 +72,7 @@
"redux-actions": "0.13.0",
"redux-api-middleware": "1.0.3",
"redux-form": "6.6.1",
"redux-sentry-middleware": "0.0.15",
"reselect": "2.5.4",
"sass-loader": "4.1.1",
"seamless-immutable": "6.1.4",
Expand Down