Skip to content

Commit

Permalink
Replace domain with uncaught exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
garethbowen committed Jan 28, 2019
1 parent dc8030e commit 125dc8f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
14 changes: 10 additions & 4 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ const db = require('./src/db-pouch'),
serverChecks = require('@shared-libs/server-checks'),
logger = require('./src/logger');

process.on('unhandledRejection', reason => {
logger.error('Unhandled Rejection:');
logger.error('%o',reason);
});
process
.on('unhandledRejection', reason => {
logger.error('UNHANDLED REJECTION!');
logger.error(' Reason: %o', reason);
})
.on('uncaughtException', err => {
logger.error('UNCAUGHT EXCEPTION!');
logger.error(' Error: %o', err);
process.exit(1);
});

serverChecks.check(db.serverUrl).then(() => {
const app = require('./src/routing'),
Expand Down
4 changes: 2 additions & 2 deletions api/src/controllers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const setCookies = (req, res, sessionRes) => {

module.exports = {
safePath: safePath,
get: (req, res) => {
get: (req, res, next) => {
const redirect = safePath(req.query.redirect);
return auth
.getUserCtx(req)
Expand All @@ -178,7 +178,7 @@ module.exports = {
renderLogin(redirect, (err, body) => {
if (err) {
logger.error('Could not find login page');
throw err;
return next(err);
}
res.send(body);
});
Expand Down
13 changes: 0 additions & 13 deletions api/src/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const _ = require('underscore'),
bulkDocs = require('./controllers/bulk-docs'),
authorization = require('./middleware/authorization'),
createUserDb = require('./controllers/create-user-db'),
createDomain = require('domain').create,
staticResources = /\/(templates|static)\//,
favicon = /\/icon_\d+.ico$/,
// CouchDB is very relaxed in matching routes
Expand Down Expand Up @@ -121,18 +120,6 @@ app.use(
})
);

app.use(function(req, res, next) {
var domain = createDomain();
domain.on('error', function(err) {
logger.error('UNCAUGHT EXCEPTION!');
serverUtils.serverError(err, req, res);
domain.dispose();
process.exit(1);
});
domain.enter();
next();
});

// requires `req` header `Accept-Encoding` to be `gzip` or `deflate`
// requires `res` `Content-Type` to be compressible (see https://github.com/jshttp/mime-db/blob/master/db.json)
// default threshold is 1KB
Expand Down

0 comments on commit 125dc8f

Please sign in to comment.