Skip to content

Instantly share code, notes, and snippets.

@chrisjensen
Created September 21, 2017 03:54
Show Gist options
  • Save chrisjensen/75d93f09fb982dc7767ec18d93c88efb to your computer and use it in GitHub Desktop.
Save chrisjensen/75d93f09fb982dc7767ec18d93c88efb to your computer and use it in GitHub Desktop.
'use strict';
const SENTRY_ID = '';
const SENTRY_KEY = '';
const SENTRY_APP = '';
const PORT = 3000;
const app = require('koa')();
const Router = require('koa-router');
// Configure Sentry
const Raven = require('raven');
const sentryUrl = `https://${SENTRY_ID}:${SENTRY_KEY}@sentry.io/${SENTRY_APP}`;
Raven.config(sentryUrl).install();
function* errors(next) {
let err;
try {
yield next;
} catch (e) {
err = e;
this.body = { message: err.message };
} finally {
Raven.captureException(
err,
{
// req: this.request,
// request: this.request,
// request: this.req,
req: this.req,
},
(sendErr, eventId) => {
if (sendErr) {
console.error('Failed to send captured exception to Sentry', err);
}
}
);
}
}
app.use(errors);
// Routes
const api = new Router();
api.get('/', function* show() { this.body = { message: 'Demo running' }; });
api.post('/error', function* makeError() { throw new Error('Testing sentry: req: this.req'); });
app.use(api.middleware());
app.listen(PORT);
console.log(`Server listening on port: ${PORT}`);
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment