express-domaining
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -26,10 +26,6 @@ /** | ||
* | ||
* @param {Object} logger | ||
* Optional logger to write errors in the domain. If not defined, it uses console as logger. | ||
* @return {Function(req, res, next)} Express middleware. | ||
*/ | ||
module.exports = function(logger) { | ||
module.exports = function() { | ||
var errorLogger = logger || console; | ||
return function domainMiddleware(req, res, next) { | ||
@@ -41,24 +37,24 @@ var requestDomain = domain.create(); | ||
requestDomain.add(res); | ||
var cleanDomain; | ||
res.on('finish', cleanDomain); | ||
requestDomain.on('error', domainErrorHandler); | ||
requestDomain.enter(); | ||
requestDomain.run(next); | ||
var domainErrorHandler = function(err) { | ||
errorLogger.error(err); | ||
function domainErrorHandler(err) { | ||
cleanDomain(); | ||
}; | ||
// Pass to the user defined error handlers in express. | ||
// This way the request can be finished with the user payloads the client may expect | ||
// and logged. | ||
next(err); | ||
// rethrow to allow cluster code or unhandledException to manage it | ||
// Usually, as this error is unexpected, the process should be terminated and restarted | ||
throw err; | ||
} | ||
var requestHandler = function() { | ||
next(); | ||
}; | ||
cleanDomain = function() { | ||
function cleanDomain() { | ||
requestDomain.removeListener('error', domainErrorHandler); | ||
res.removeListener('finish', cleanDomain); | ||
requestDomain.exit(); | ||
}; | ||
res.on('finish', cleanDomain); | ||
requestDomain.on('error', domainErrorHandler); | ||
requestDomain.enter(); | ||
requestDomain.run(requestHandler); | ||
} | ||
}; | ||
}; |
{ | ||
"name": "express-domaining", | ||
"description": "Express middleware to create and destroy a node domain", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"license": "Apache-2.0", | ||
@@ -13,3 +13,4 @@ "author": { | ||
"Guido García Bernardo <guido.garciabernardo@telefonica.com>", | ||
"David Lozano Llanos <david.lozanollanos@telefonica.com>" | ||
"David Lozano Llanos <david.lozanollanos@telefonica.com>", | ||
"Javier Mendiara Cañardo <javier.mendiaracanardo@telefonica.com>" | ||
], | ||
@@ -16,0 +17,0 @@ "repository": { |
@@ -5,2 +5,7 @@ # express-domaining | ||
[READ THIS BEFORE USING THIS MODULE!](https://nodejs.org/api/domain.html#domain_domain) | ||
Allows you to respond to requests that had and unexpected error using your express error handlers and | ||
rethrows the exception to be captured by your own unhandledExcetion/cluster code | ||
[![npm version](https://badge.fury.io/js/express-domaining.svg)](http://badge.fury.io/js/express-domaining) | ||
@@ -28,15 +33,2 @@ [![Build Status](https://travis-ci.org/telefonica/node-express-domaining.svg)](https://travis-ci.org/telefonica/node-express-domaining) | ||
By default, errors in domain are traced by `console.error`. However, you can use a custom logger: | ||
```js | ||
var express = require('express'), | ||
expressDomain = require('express-domaining'), | ||
logger = require('logops'); | ||
var app = express(); | ||
app.use(expressDomain(logger)); | ||
app.listen(3000); | ||
``` | ||
## License | ||
@@ -43,0 +35,0 @@ |
@@ -84,4 +84,11 @@ 'use strict'; | ||
}); | ||
var unhandlerError = new Error('test error'); | ||
// get mocha exception handler | ||
var originalException = process.listeners('uncaughtException').pop() | ||
// and remove it | ||
process.removeListener('uncaughtException', originalException); | ||
var domainMiddleware = new DomainMiddleware(); | ||
domainMiddleware(req, res, function onNext() { | ||
domainMiddleware(req, res, function onNext(propagatedError) { | ||
expect(domainSpy.enter.calledOnce).to.be.true; | ||
@@ -91,8 +98,17 @@ expect(domainSpy.run.calledOnce).to.be.true; | ||
expect(domainSpy.add.withArgs(res).calledOnce).to.be.true; | ||
if (propagatedError) { | ||
process.once('uncaughtException', function(err) { | ||
// restore mocha error handler | ||
process.listeners('uncaughtException').push(originalException) | ||
expect(err).to.be.eql(unhandlerError) | ||
expect(propagatedError).to.be.eql(unhandlerError) | ||
expect(domainSpy.remove.withArgs(req).notCalled).to.be.true; | ||
expect(domainSpy.remove.withArgs(res).notCalled).to.be.true; | ||
expect(domainSpy.exit.calledOnce).to.be.true; | ||
done(); | ||
}); | ||
} | ||
// Trigger error | ||
domain.emit('error', new Error('test error')); | ||
expect(domainSpy.remove.withArgs(req).notCalled).to.be.true; | ||
expect(domainSpy.remove.withArgs(res).notCalled).to.be.true; | ||
expect(domainSpy.exit.calledOnce).to.be.true; | ||
done(); | ||
domain.emit('error', unhandlerError); | ||
}); | ||
@@ -99,0 +115,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
166
1
20796
12
41