Skip to content

Commit 4e36116

Browse files
committed
allow message to be specified as an arg.
1 parent df69b59 commit 4e36116

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

lib/util/makeErrorFunction.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,20 @@ module.exports = function makeErrorFunction(errorCodeName, errorCodeSpec) {
1010
return function () {
1111
var error = new Error(""), message;
1212
error.id = "myId";
13-
error.internal = {nodeErrors:true};
1413
error.name = errorCodeName;
14+
//get error message
15+
if (errorCodeSpec.args) {
16+
var numberOfFormatArguments = (errorCodeSpec.message.match(/\%s|\%d|\%j/g) || []).length;
17+
var formatArguments = [errorCodeSpec.message];
18+
var nonInternalArguments = Array.prototype.slice.call(arguments, 0, numberOfFormatArguments);
19+
formatArguments.push.apply(formatArguments, nonInternalArguments);
20+
message = format.apply(format, formatArguments);
21+
} else {
22+
message = errorCodeSpec.message;
23+
}
24+
error.message = message;
25+
error.id = uuid.v4();
26+
//add arguments
1527
var i = 0;
1628
if (errorCodeSpec.args) {
1729
for (i = 0; i < errorCodeSpec.args.length; i++) {
@@ -24,17 +36,8 @@ module.exports = function makeErrorFunction(errorCodeName, errorCodeSpec) {
2436
if(arguments[i]){
2537
error.internal = arguments[i]; //always allow additional last argument to be stored as internal
2638
}
27-
//get error message
28-
if (errorCodeSpec.args) {
29-
var formatArguments = [errorCodeSpec.message];
30-
var nonInternalArguments = Array.prototype.slice.call(arguments, 0, errorCodeSpec.args.length);
31-
formatArguments.push.apply(formatArguments, nonInternalArguments);
32-
message = format.apply(format, formatArguments);
33-
} else {
34-
message = errorCodeSpec.message;
35-
}
36-
error.message = message;
37-
error.id = uuid.v4();
39+
error.internal = error.internal || {};
40+
error.internal.nodeErrors = true;
3841
//add additional error code spec properties
3942
var properties = Object.keys(errorCodeSpec);
4043
for(i=0; i<properties.length; i++){

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Allan Ebdrup",
33
"name": "nodeerrors",
44
"description": "error handling module for node",
5-
"version": "1.1.1",
5+
"version": "1.1.2",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/Muscula/nodeerrors"

0 commit comments

Comments
 (0)