feat(api): enable Sentry reporting

This commit is contained in:
Oliver Eyton-Williams
2020-03-23 18:31:25 +01:00
committed by Mrugesh Mohapatra
parent 61e5bf942b
commit 10a6622546
7 changed files with 47 additions and 7 deletions

View File

@ -7,14 +7,26 @@ const loopback = require('loopback');
const boot = require('loopback-boot'); const boot = require('loopback-boot');
const expressState = require('express-state'); const expressState = require('express-state');
const createDebugger = require('debug'); const createDebugger = require('debug');
const Sentry = require('@sentry/node');
const { sentry } = require('../../config/secrets');
const { setupPassport } = require('./component-passport'); const { setupPassport } = require('./component-passport');
const log = createDebugger('fcc:server'); const log = createDebugger('fcc:server');
// force logger to always output // force logger to always output
// this may be brittle // this may be brittle
log.enabled = true; log.enabled = true;
if (sentry.dns === 'dsn_from_sentry_dashboard') {
log('Sentry reporting disabled unless DSN is provided.');
} else {
Sentry.init({
dsn: sentry.dns
});
log('Sentry initialized');
}
Rx.config.longStackSupport = process.env.NODE_DEBUG !== 'production'; Rx.config.longStackSupport = process.env.NODE_DEBUG !== 'production';
const app = loopback(); const app = loopback();

View File

@ -1,5 +1,6 @@
{ {
"initial:before": { "initial:before": {
"./middlewares/sentry-request-handler": {},
"loopback#favicon": { "loopback#favicon": {
"params": "$!../public/favicon.ico" "params": "$!../public/favicon.ico"
}, },
@ -59,7 +60,7 @@
}, },
"files": {}, "files": {},
"final:after": { "final:after": {
"./middlewares/error-reporter": {}, "./middlewares/sentry-error-handler": {},
"./middlewares/error-handlers": {}, "./middlewares/error-handlers": {},
"strong-error-handler": { "strong-error-handler": {
"params": { "params": {

View File

@ -0,0 +1,19 @@
import { Handlers, captureException } from '@sentry/node';
import { sentry } from '../../../config/secrets';
export function reportError(err) {
return sentry.dns === 'dsn_from_sentry_dashboard'
? console.error(err)
: captureException(err);
}
export default function sentryErrorHandler() {
return sentry.dns === 'dsn_from_sentry_dashboard'
? (req, res, next) => next()
: Handlers.errorHandler({
shouldHandleError(error) {
// NOTE: 400 is too low, this is just for debugging
return !error.status || error.status >= 400;
}
});
}

View File

@ -0,0 +1,8 @@
import { Handlers } from '@sentry/node';
import { sentry } from '../../../config/secrets';
export default function sentryRequestHandler() {
return sentry.dns === 'dsn_from_sentry_dashboard'
? (req, res, next) => next()
: Handlers.requestHandler();
}

View File

@ -1,7 +1,7 @@
import { Observable } from 'rx'; import { Observable } from 'rx';
import debug from 'debug'; import debug from 'debug';
import { reportError } from '../middlewares/error-reporter'; import { reportError } from '../middlewares/sentry-error-handler.js';
import InMemoryCache from '../utils/in-memory-cache'; import InMemoryCache from '../utils/in-memory-cache';
const log = debug('fcc:boot:donate'); const log = debug('fcc:boot:donate');

View File

@ -27,8 +27,7 @@ const {
TWITTER_TOKEN, TWITTER_TOKEN,
TWITTER_TOKEN_SECRET, TWITTER_TOKEN_SECRET,
ROLLBAR_APP_ID, SENTRY_DSN,
ROLLBAR_CLIENT_ID,
STRIPE_PUBLIC_KEY, STRIPE_PUBLIC_KEY,
STRIPE_SECRET_KEY, STRIPE_SECRET_KEY,
@ -95,9 +94,8 @@ module.exports = {
passReqToCallback: true passReqToCallback: true
}, },
rollbar: { sentry: {
appId: ROLLBAR_APP_ID, dns: SENTRY_DSN
clientId: ROLLBAR_CLIENT_ID
}, },
stripe: { stripe: {

View File

@ -53,3 +53,5 @@ TEST_CHALLENGES_FOR_LANGS=english
DOCKER_HOST_LOCATION=localhost DOCKER_HOST_LOCATION=localhost
GHOST_CLIENT_KEY=123abc GHOST_CLIENT_KEY=123abc
SENTRY_DSN=dsn_from_sentry_dashboard