From 652fe5c264c4d2b31b938a4e80012fd1e6808cec Mon Sep 17 00:00:00 2001 From: Jonathan Cremin Date: Sun, 23 Aug 2015 01:05:20 +0100 Subject: [PATCH] Fix events stuff, improve errors --- api/app.js | 5 +++++ api/routes/user.js | 21 ++++++++++++++------- app.js | 27 +++++++++++++++++++++++---- web/app.js | 1 + web/public/50x.html | 8 ++++---- web/public/error.html | 2 +- 6 files changed, 48 insertions(+), 16 deletions(-) diff --git a/api/app.js b/api/app.js index 3fef967..071158a 100644 --- a/api/app.js +++ b/api/app.js @@ -46,6 +46,7 @@ router.use('/*',function* (next) { } else { if (!err.status) { debug(err); + this.raven.captureError(err); throw err; } else { this.status = err.status; @@ -73,4 +74,8 @@ router.get('/(.*)', function* () { this.throw(404); }); +export const ws = new Router(); + +ws.all('/user', user.events); + export default router; diff --git a/api/routes/user.js b/api/routes/user.js index 06eea43..5142439 100644 --- a/api/routes/user.js +++ b/api/routes/user.js @@ -65,16 +65,23 @@ export function* events() { let json; try{ json = JSON.parse(message); - } catch(e) { + } catch(err) { debug('Invalid JSON for socket auth'); this.websocket.send('Invalid authentication message. Bad JSON?'); + this.raven.captureError(err); } - const reply = yield this.redis.get(json.authorization); - if (reply) { - pubsub.subscribe('/user/' + reply); - debug('Subscribed to: /user/%s', reply); - } else { - this.websocket.send('Invalid authentication token.'); + try { + const reply = yield this.redis.get(json.authorization); + if (reply) { + pubsub.subscribe('/user/' + reply); + this.websocket.send('{"status":"active"}'); + debug('Subscribed to: /user/%s', reply); + } else { + this.websocket.send('Invalid authentication token.'); + } + } catch(err) { + debug(err); + this.raven.captureError(err); } }.bind(this))); }); diff --git a/app.js b/app.js index cf8c90e..633e1b2 100644 --- a/app.js +++ b/app.js @@ -15,6 +15,7 @@ import mongo from './lib/mongo'; import * as redis from './lib/redis'; import co from 'co'; import api from './api/app'; +import { ws } from './api/app'; import web from './web/app'; import { init as storageInit } from './lib/storage'; storageInit(); @@ -22,14 +23,22 @@ storageInit(); import debugname from 'debug'; const debug = debugname('hostr'); +const app = websockify(koa()); +app.keys = [process.env.KEYS || 'INSECURE']; + if (process.env.SENTRY_DSN) { const ravenClient = new raven.Client(process.env.SENTRY_DSN); ravenClient.patchGlobal(); + app.use(function* (next) { + this.raven = ravenClient; + yield next; + }); + app.ws.use(function* (next) { + this.raven = ravenClient; + yield next; + }); } -const app = websockify(koa()); -app.keys = [process.env.KEYS || 'INSECURE']; - app.use(helmet()); app.use(function* (next){ @@ -37,7 +46,14 @@ app.use(function* (next){ if(this.req.headers['x-forwarded-proto'] === 'http'){ return this.redirect('https://' + this.req.headers.host + this.req.url); } - yield next; + try { + yield next; + } catch (err) { + if (!err.statusCode) { + this.raven.captureError(err); + } + throw err; + } }); app.use(mongo()); @@ -52,6 +68,9 @@ app.use(serve(path.join(__dirname, 'web/public/'), {maxage: 31536000000})); app.use(api.prefix('/api').routes()); app.use(web.prefix('').routes()); +app.ws.use(redis.middleware()); +app.ws.use(ws.prefix('/api').routes()); + if (!module.parent) { app.listen(process.env.PORT || 4040, function() { debug('Koa HTTP server listening on port ' + (process.env.PORT || 4040)); diff --git a/web/app.js b/web/app.js index df57e3f..a5ebc52 100644 --- a/web/app.js +++ b/web/app.js @@ -17,6 +17,7 @@ import debugname from 'debug'; const debug = debugname('hostr-web'); const router = new Router(); + router.use(errors({template: path.join(__dirname, 'public', 'error.html')})); let statsdOpts = {prefix: 'hostr-web', host: process.env.STATSD_HOST || 'localhost'}; diff --git a/web/public/50x.html b/web/public/50x.html index 8decaf1..1bf216b 100644 --- a/web/public/50x.html +++ b/web/public/50x.html @@ -4,7 +4,7 @@ - Hostr - File not found + Hostr - 50x Unexpected Error @@ -21,9 +21,9 @@
-

500

-

Sorry, It looks like you've hit an unexpected error.

-

Refreshing might fix the problem. If not, sit tight! We're on it!

+

50x

+

Sorry, it looks like you've hit an unexpected error.

+

Refreshing might fix the problem. If not, sit tight! We're on it!

diff --git a/web/public/error.html b/web/public/error.html index 1de9e73..d9f0a73 100644 --- a/web/public/error.html +++ b/web/public/error.html @@ -4,7 +4,7 @@ - Hostr - File not found + Hostr - Error Encountered