-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmakeErrorFunction.spec.js
More file actions
134 lines (118 loc) · 4.22 KB
/
makeErrorFunction.spec.js
File metadata and controls
134 lines (118 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"use strict";
describe("When making an error function", function () {
var makeErrorFunction = require("../../lib/util/makeErrorFunction");
describe("when there is no name specified", function () {
it("will give throw an error", function () {
var errorCodeSpec = {
message:"There was an internal server error"
};
expect(function () {
makeErrorFunction("", errorCodeSpec);
}).to["throw"]("The error code specification has no name");
expect(function () {
makeErrorFunction(null, errorCodeSpec);
}).to["throw"]("The error code specification has no name");
expect(function () {
makeErrorFunction(undefined, errorCodeSpec);
}).to["throw"]("The error code specification has no name");
});
});
describe("with no arguments", function () {
it("will have a call stack", function () {
var errorCodeSpec = {
message:"There was an internal server error"
};
var fn = makeErrorFunction("system", errorCodeSpec);
var error = fn();
expect(error.stack).to.be.ok;
});
it("will give it the correct code, id and message", function () {
var errorCodeSpec = {
message:"There was an internal server error"
};
var fn = makeErrorFunction("system", errorCodeSpec);
var errorObject = fn();
expect(errorObject.code).to.equal("system");
expect(errorObject.id).to.be.a("string");
expect(errorObject.message).to.equal(errorCodeSpec.message);
});
it("will add properties from spec to error", function () {
var errorCodeSpec = {
message:"There was an internal server error",
http: 123456
};
var fn = makeErrorFunction("system", errorCodeSpec);
var errorObject = fn();
expect(errorObject.http).to.equal(123456);
});
it("will store argument passed as 'internal' property", function () {
var internalData = { test:true, myArray: [1,2,3]};
var errorCodeSpec = {
message:"There was an internal server error"
};
var fn = makeErrorFunction("test", errorCodeSpec);
var errorObject = fn(internalData);
expect(errorObject.internal).to.eql(internalData);
});
});
describe("with arguments", function () {
it("will format the message with one argument", function () {
var errorCodeSpec = {
message:"error %s",
args:["myParameter"]
};
var fn = makeErrorFunction("test", errorCodeSpec);
var errorObject = fn("testParameterValue");
expect(errorObject.message).to.equal("error testParameterValue");
});
it("will format the message with one argument and an internal parameter", function () {
var internalData = { test:true, myArray: [1,2,3]};
var errorCodeSpec = {
message:"error %s",
args:["myParameter"]
};
var fn = makeErrorFunction("test", errorCodeSpec);
var errorObject = fn("testParameterValue", internalData);
expect(errorObject.message).to.equal("error testParameterValue");
expect(errorObject.internal).to.eql(internalData);
});
it("will format the message with two arguments", function () {
var errorCodeSpec = {
message:"error %s, %s",
args:["myParameter1", "myParameter2"]
};
var fn = makeErrorFunction("test", errorCodeSpec);
var errorObject = fn("testParameterValue1", "testParameterValue2");
expect(errorObject.message).to.equal("error testParameterValue1, testParameterValue2");
});
it("will add the parameter to json in error object message", function () {
var errorCodeSpec = {
message:"error %s",
args:["myParameter"]
};
var fn = makeErrorFunction("test", errorCodeSpec);
var errorObject = fn("testParameterValue");
expect(errorObject.myParameter).to.equal("testParameterValue");
});
it("will accept an object as parameter", function () {
var data = { test:true, myArray: [1,2,3]};
var errorCodeSpec = {
message:"error %s",
args:["myParameter"]
};
var fn = makeErrorFunction("test", errorCodeSpec);
var errorObject = fn(data);
expect(errorObject.myParameter).to.eql(data);
});
it("will store additional last parameter as 'internal' property", function () {
var data = { test:true, myArray: [1,2,3]};
var errorCodeSpec = {
message:"error %s",
args:["myParameter"]
};
var fn = makeErrorFunction("test", errorCodeSpec);
var errorObject = fn("test", data);
expect(errorObject.internal).to.eql(data);
});
});
});