Comparing version 1.0.0 to 1.0.1
119
lib/index.js
'use strict'; | ||
/** | ||
* @module fi-errors | ||
* @version 1.0.0 | ||
* @author Leonardo Ramos <leonardo@finaldevstudio.com>, Santiago G. Marín <santiago@finaldevstudio.com> | ||
* @copyright Final Development Studio 2017 | ||
* @license MIT | ||
* | ||
* @overview This component acts as the very last middleware of an express application. It manages every error may be produced inside any of the previous express middlewares. | ||
**/ | ||
const defaults = require('./defaults'); | ||
@@ -8,8 +18,6 @@ | ||
/** | ||
* The component configuration. | ||
*/ | ||
const config = {}; | ||
/** | ||
* Every custom error function. | ||
* | ||
* @type {Object} | ||
* @private | ||
*/ | ||
@@ -20,2 +28,5 @@ const errors = {}; | ||
* Every redirect url. | ||
* | ||
* @type {Object} | ||
* @private | ||
*/ | ||
@@ -26,2 +37,5 @@ const redirect = {}; | ||
* Every failed HTTP request to this urls will be terminated | ||
* | ||
* @type {String} | ||
* @private | ||
*/ | ||
@@ -31,6 +45,19 @@ var exclude; | ||
/** | ||
* Checks if the component has been configured. | ||
* | ||
* @type {Boolean} | ||
* @private | ||
*/ | ||
var initialized = {}; | ||
/** | ||
* Builds an error function. | ||
* | ||
* @private | ||
* @param {Object} options The error definition. | ||
* @returns An error function. | ||
* @param {String} options.name The error name. | ||
* @param {String} options.message The error default message. | ||
* @param {String} options.code The error HTTP response code. | ||
* | ||
* @returns {Function} An error function. | ||
*/ | ||
@@ -63,2 +90,3 @@ function _buildError(options) { | ||
* | ||
* @private | ||
* @param {Array} _errors An array of custom errors. | ||
@@ -88,2 +116,3 @@ */ | ||
* | ||
* @private | ||
* @param {Object} _redirect Contains the error and lost redirection urls. | ||
@@ -110,2 +139,3 @@ */ | ||
* | ||
* @private | ||
* @param {Regex} _exclude Every failed HTTP request to this urls will be terminated | ||
@@ -120,2 +150,3 @@ */ | ||
* | ||
* @private | ||
* @param {Object} req The request object. | ||
@@ -132,2 +163,3 @@ * @param {Object} res The response object. | ||
* | ||
* @private | ||
* @param {Error} err The generated error. | ||
@@ -151,4 +183,7 @@ * @param {Object} req The request object. | ||
/* If the request is an AJAX call or is for an asset or API method just end | ||
* the response */ | ||
/** | ||
* If the request is an AJAX call or is for an asset or API method | ||
* just end the response | ||
* | ||
*/ | ||
if (req.xhr || exclude.test(req.path)) { | ||
@@ -162,3 +197,3 @@ return res.end(); | ||
} | ||
res.redirect(redirect.error); | ||
@@ -170,19 +205,20 @@ } | ||
/** | ||
* Initialize and configure the errors component | ||
* Initialize and configure the errors component. | ||
* | ||
* @param {Object} cfg The errors configuration object. | ||
* @param {Array} cfg.errors The custom errors configuration array file. | ||
* @param {Object} cfg.redirect The default redirect urls. | ||
* @param {Object} config The errors configuration object. | ||
* @param {Array} config.errors The custom errors configuration array file. | ||
* @param {Object} config.redirect The default redirect urls. | ||
* @param {String} config.exclude The urls that if failed will terminate the request. | ||
* | ||
* @returns The errors component. | ||
* @returns {Object} The errors component. | ||
*/ | ||
configure: function configure(cfg) { | ||
cfg = cfg || {}; | ||
configure: function configure(config) { | ||
config = config || {}; | ||
_loadErrors(cfg.errors); | ||
_loadExclusions(cfg.exclude); | ||
_loadRedirections(cfg.redirect); | ||
_loadErrors(config.errors); | ||
_loadExclusions(config.exclude); | ||
_loadRedirections(config.redirect); | ||
config.initialized = true; | ||
initialized = true; | ||
@@ -193,14 +229,15 @@ return this; | ||
/** | ||
* Register the custom errors list in the application global object. | ||
* Bind the errors component to the express aplication. | ||
* | ||
* @param {Object} global The application global object. | ||
* @param {Express} app The express application. | ||
* | ||
* @returns The errors component. | ||
* @returns {Object} The errors component. | ||
*/ | ||
register: function register(global) { | ||
if (!config.initialized) { | ||
bind: function bind(app) { | ||
if (!initialized) { | ||
this.configure(); | ||
} | ||
global[config.globalRegisterName] = errors; | ||
app.use(_notFoundErrorHandler); | ||
app.use(_customErrorHandler); | ||
@@ -211,30 +248,32 @@ return this; | ||
/** | ||
* Bind the errors component to the express aplication. | ||
* Returns the component errors list. | ||
* | ||
* @param {Express} app The express application. | ||
* | ||
* @returns The errors component. | ||
* @returns {Object} Every registered error. | ||
*/ | ||
bind: function bind(app) { | ||
if (!config.initialized) { | ||
list: function list() { | ||
if (!initialized) { | ||
this.configure(); | ||
} | ||
app.use(_notFoundErrorHandler); | ||
app.use(_customErrorHandler); | ||
return this; | ||
return errors; | ||
}, | ||
/** | ||
* @returns Every registered error. | ||
* Register the custom errors list in the application global object. | ||
* | ||
* @param {Object} global The application global object. | ||
* @param {String} attribute The name of the attribute to register in the global object. | ||
* | ||
* @returns {Object} The errors component. | ||
*/ | ||
list: function list() { | ||
if (!config.initialized) { | ||
register: function register(global, attribute) { | ||
if (!initialized) { | ||
this.configure(); | ||
} | ||
return errors; | ||
global[attribute] = errors; | ||
return this; | ||
} | ||
}; |
{ | ||
"name": "fi-errors", | ||
"title": "Fi Errors", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Error manangement middleware for ExpressJS", | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
21294
17
509
1
104