|
1 | 1 | "use strict"; |
2 | | -var _ = require("underscore"); |
3 | | -var errorCodeSpecs = require("./util/getErrorConfig"); |
| 2 | +delete require.cache[__filename]; //do not cache in require cache |
| 3 | +var getErrorConfig = require("./util/getErrorConfig"); |
4 | 4 | var makeErrorFunction = require("./util/makeErrorFunction"); |
| 5 | +var getErrorConfigFilePath = require("./util/getErrorConfigFilePath"); |
5 | 6 | var parse = require("./util/parse"); |
6 | 7 | var middleware = require("./middleware"); |
| 8 | +var cache = require("./util/cache"); //use requires caching to have a singleton |
| 9 | +var path = require("path"); |
7 | 10 |
|
8 | | -var nodeError = { |
9 | | - errorCodes:{}, |
10 | | - parse:parse, |
11 | | - middleware:middleware |
12 | | -}; |
13 | | - |
14 | | -var functionsToMake = _.keys(errorCodeSpecs); //get array of friendly names. |
15 | | -for (var i = 0; i < functionsToMake.length; i++) { |
16 | | - //get error code friendly name, for example: "internalError" |
17 | | - var errorCodeName = functionsToMake[i]; |
18 | | - //make error function for friendly name |
19 | | - nodeError[errorCodeName] = makeErrorFunction(errorCodeName, errorCodeSpecs[errorCodeName]); |
20 | | - //save the error code in errorCodes |
21 | | - nodeError.errorCodes[errorCodeName] = errorCodeName; |
22 | | -} |
| 11 | +function 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 | + } |
23 | 26 |
|
24 | | -Error.prototype.innerError = function addInnerError(err) { |
25 | | - this.internal = this.internal || {}; |
26 | | - this.internal.innerError = err; |
27 | | - return this; |
28 | | -}; |
| 27 | + if (!cache.prototypesExtended) { |
| 28 | + cache.prototypesExtended = true; |
| 29 | + Error.prototype.innerError = function addInnerError(err) { |
| 30 | + this.internal = this.internal || {}; |
| 31 | + this.internal.innerError = err; |
| 32 | + return this; |
| 33 | + }; |
| 34 | + Function.prototype.onError = function (callback) { |
| 35 | + var fn = this; |
| 36 | + return function (err) { |
| 37 | + if (err) { |
| 38 | + return callback(err); |
| 39 | + } |
| 40 | + try { |
| 41 | + fn.apply(null, arguments); |
| 42 | + } catch (ex) { |
| 43 | + return callback(ex); |
| 44 | + } |
| 45 | + }; |
| 46 | + }; |
| 47 | + } |
29 | 48 |
|
30 | | -Function.prototype.onError = function (callback) { |
31 | | - var fn = this; |
32 | | - return function (err) { |
33 | | - if (err) { |
34 | | - return callback(err); |
35 | | - } |
36 | | - try { |
37 | | - fn.apply(null, arguments); |
38 | | - } catch (ex) { |
39 | | - return callback(ex); |
40 | | - } |
| 49 | + //Not in cache, create nodeErrors object |
| 50 | + var nodeError = { |
| 51 | + errorCodes:{}, |
| 52 | + middleware:middleware |
41 | 53 | }; |
42 | | -}; |
| 54 | + nodeError.parse = parse.bind(nodeError); |
| 55 | + var errorCodeSpecs = getErrorConfig(errorConfigFilePath); |
| 56 | + |
| 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 | + } |
| 66 | + |
| 67 | + cache.nodeerrors[errorConfigFilePath] = nodeError; |
| 68 | + return nodeError; |
| 69 | +} |
43 | 70 |
|
44 | | -module.exports = nodeError; |
| 71 | +module.exports = getNodeErrors(); |
0 commit comments