Comparing version 0.2.1 to 0.2.2
@@ -216,4 +216,4 @@ "use strict"; | ||
//Express try-catches every middleware | ||
//When a user's middleware throws an exception the request error handler is | ||
//called. If this handler throws or it simply falls back to the default error | ||
//When a middleware throws an exception the request error handler is called | ||
//If this handler throws or it simply falls back to the default error | ||
//handler and it throws again, that is, exceptions that are thrown in the | ||
@@ -237,5 +237,5 @@ //default error handler, Express try-catches them and the program is still | ||
Grace.prototype._callErrorHandler = function (error, req, res, cb){ | ||
//If this function is called more than one that means that an asynchronous | ||
//exception has been thrown inside the request error handler and generates | ||
//infinite recursive calls, e.g.: | ||
//If this function is called more than one time that means that an | ||
//asynchronous exception has been thrown inside the request error handler and | ||
//generates infinite recursive calls, e.g.: | ||
//errorHandler (function (error, req, res, preventDefault){ | ||
@@ -286,4 +286,4 @@ // process.nextTick (function (){ null.killer }); | ||
//Express middleware for handling uncaught exceptions thrown by a request | ||
Grace.prototype.errorHandler = function (cb){ | ||
//First Express middleware for handling uncaught exceptions thrown by a request | ||
var me = this; | ||
@@ -315,12 +315,20 @@ return function (req, res, next){ | ||
Grace.prototype.redirectError = function (error, req, res){ | ||
//Normal use | ||
var argsLen = arguments.length; | ||
//Redirect to the default error handler | ||
if (argsLen === 1){ | ||
return this._emitError (error); | ||
} | ||
//Redirect to the request error handler and fall back to the default | ||
//Normal, with any framework | ||
//e.use (function (error, req, res, next){ | ||
// g.redirectError (error, req, res); | ||
//}); | ||
if (arguments.length === 3){ | ||
this._callErrorHandler (error, req, res, req._grace.errorHandler); | ||
return; | ||
if (argsLen === 3){ | ||
return this._callErrorHandler (error, req, res, req._grace.errorHandler); | ||
} | ||
//Express shorthand | ||
//Shorthand, Express error handler, last middleware | ||
//e.use (g.redirectError ()); | ||
@@ -327,0 +335,0 @@ var me = this; |
{ | ||
"name": "grace", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Graceful application with domains, cluster, error handling and Express support", | ||
@@ -5,0 +5,0 @@ "keywords": ["graceful", "grace", "shutdown", "restart", "domain", "error", |
@@ -14,3 +14,3 @@ grace | ||
Version: 0.2.1 | ||
Version: 0.2.2 | ||
@@ -76,3 +76,3 @@ Provides an event-based mechanism to start and gracefully shutdown a web server. | ||
Take a look at the [examples](https://github.com/Gagle/Node-GracefulShut/blob/master/examples) to fully understand how to use a "graceful application" -especially with workers and Express-. Once you feel comfortable with it you probably will never stop using it because it provides the base of a robust web server. | ||
Take a look at the [examples](https://github.com/Gagle/Node-Grace/blob/master/examples) to fully understand how to use a "graceful application" -especially with workers and Express-. Once you feel comfortable with it you probably will never stop using it because it provides the base of a robust web server. | ||
@@ -82,3 +82,3 @@ - [gs.create()](#create) | ||
- [Grace#errorHandler([callback])](#errorHandler) | ||
- [Grace#redirectError([error, request, response])](#redirectError) | ||
- [Grace#redirectError([error[, request, response]])](#redirectError) | ||
- [Grace#shutdown([exitCode])](#shutdown) | ||
@@ -122,17 +122,37 @@ - [Grace#start()](#start) | ||
<a name="redirectError"></a> | ||
__Grace#redirectError([error, request, response])__ | ||
Used with Express in its error handler. Redirects the error to the request error handler and falls back to the default error handler. The Express error handler should the last middleware. | ||
__Grace#redirectError([error[, request, response]])__ | ||
Redirects an error to an error handler. It redirects to the default or request error handler depending on the number of parameters. | ||
There are 2 ways to redirect the Express error: | ||
- 0 parameters. | ||
Express -or any frameworks express-like- is required. It's a shorthand to use the Express error handler. | ||
```javascript | ||
//Shorthand | ||
ex.use (g.redirectError ()); | ||
```javascript | ||
//Express error handler, last middleware | ||
ex.use (g.redirectError ()); | ||
``` | ||
//If you need to do anything before redirecting | ||
ex.use (function (error, req, res, next){ | ||
g.redirectError (error, req, res); | ||
}); | ||
``` | ||
- 1 parameter: error. | ||
Redirects to the default error handler. Useful when you need to do something before redirecting to the default error handler. | ||
```javascript | ||
//This redirects errors to the default error handler but you can't do anything before redirecting | ||
asyncFUnction (g.dom ().intercept ()); | ||
//Solution, use redirectError() | ||
asyncFunction (function (error){ | ||
doSomething (); | ||
g.redirectError (error); | ||
}); | ||
``` | ||
- 3 parameters: error, request, response. | ||
Redirects to the request error handler and falls back to the default error handler if `preventDefault()` is not called. Useful when you need to do something before redirecting to the request error handler. It can be used inside the Express error handler. | ||
```javascript | ||
//Express error handler, last middleware | ||
ex.use (function (error, req, res, next){ | ||
g.redirectError (error, req, res); | ||
}); | ||
``` | ||
<a name="shutdown"></a> | ||
@@ -185,2 +205,2 @@ __Grace#shutdown([exitCode])__ | ||
__start__ | ||
Emitted right after the `start()` function is called. | ||
Emitted right after the `start()` function is called. |
26629
430
202