11"use strict" ;
2- delete require . cache [ __filename ] ; //do not cache in require cache
32var getErrorConfig = require ( "./util/getErrorConfig" ) ;
43var makeErrorFunction = require ( "./util/makeErrorFunction" ) ;
5- var getErrorConfigFilePath = require ( "./util/getErrorConfigFilePath " ) ;
4+ var moduleConfig = require ( "moduleconfig " ) ;
65var parse = require ( "./util/parse" ) ;
76var middleware = require ( "./middleware" ) ;
8- var cache = require ( "./util/cache" ) ; //use requires caching to have a singleton
9- var path = require ( "path" ) ;
107
118function getNodeErrors ( ) {
12- var startDir = path . dirname ( module . parent . filename ) ;
13- var errorConfigFilePath ;
14- if ( cache . errorConfigFilePaths [ startDir ] ) {
15- errorConfigFilePath = cache . errorConfigFilePaths [ startDir ] ;
16- } else {
17- errorConfigFilePath = getErrorConfigFilePath ( startDir ) ;
18- if ( ! errorConfigFilePath ) {
19- throw new Error ( "nodeerrors, cannot find .errors.js, starting from path " + startDir ) ;
20- }
21- cache . errorConfigFilePaths [ startDir ] = errorConfigFilePath ;
22- }
23- if ( cache . nodeerrors [ errorConfigFilePath ] ) {
24- return cache . nodeerrors [ errorConfigFilePath ] ;
25- }
26-
27- if ( ! cache . prototypesExtended ) {
28- cache . prototypesExtended = true ;
9+ if ( ! moduleConfig . prototypesExtended ) {
10+ moduleConfig . prototypesExtended = true ;
2911 Error . prototype . innerError = function addInnerError ( err ) {
3012 this . internal = this . internal || { } ;
3113 this . internal . innerError = err ;
@@ -46,26 +28,27 @@ function getNodeErrors() {
4628 } ;
4729 }
4830
49- //Not in cache, create nodeErrors object
50- var nodeError = {
51- errorCodes :{ } ,
52- middleware :middleware
53- } ;
54- nodeError . parse = parse . bind ( nodeError ) ;
55- var errorCodeSpecs = getErrorConfig ( errorConfigFilePath ) ;
5631
57- var functionsToMake = Object . keys ( errorCodeSpecs ) ; //get array of friendly names.
58- for ( var i = 0 ; i < functionsToMake . length ; i ++ ) {
59- //get error code friendly name, for example: "internalError"
60- var errorCodeName = functionsToMake [ i ] ;
61- //make error function for friendly name
62- nodeError [ errorCodeName ] = makeErrorFunction ( errorCodeName , errorCodeSpecs [ errorCodeName ] ) ;
63- //save the error code in errorCodes
64- nodeError . errorCodes [ errorCodeName ] = errorCodeName ;
65- }
32+ return moduleConfig ( [ ".errors.js" , "config/.errors.js" ] , function ( errorConfigFilePath ) {
33+ //Not in cache, create nodeErrors object
34+ var nodeError = {
35+ errorCodes :{ } ,
36+ middleware :middleware
37+ } ;
38+ nodeError . parse = parse . bind ( nodeError ) ;
39+ var errorCodeSpecs = getErrorConfig ( errorConfigFilePath ) ;
40+ var functionsToMake = Object . keys ( errorCodeSpecs ) ; //get array of friendly names.
41+ for ( var i = 0 ; i < functionsToMake . length ; i ++ ) {
42+ //get error code friendly name, for example: "internalError"
43+ var errorCodeName = functionsToMake [ i ] ;
44+ //make error function for friendly name
45+ nodeError [ errorCodeName ] = makeErrorFunction ( errorCodeName , errorCodeSpecs [ errorCodeName ] ) ;
46+ //save the error code in errorCodes
47+ nodeError . errorCodes [ errorCodeName ] = errorCodeName ;
48+ }
49+ return nodeError ;
50+ } ) ;
6651
67- cache . nodeerrors [ errorConfigFilePath ] = nodeError ;
68- return nodeError ;
6952}
7053
7154module . exports = getNodeErrors ( ) ;
0 commit comments