This repository has been archived by the owner on Mar 18, 2020. It is now read-only.
/
errors.js
195 lines (173 loc) · 6.67 KB
/
errors.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const assert = require('assert')
const strictErrors = require('../src')
const createErrorDefinitions = strictErrors.createErrorDefinitions
const StrictError = strictErrors.StrictError
const errorsToJson = strictErrors.errorsToJson
const DEFINITIONS = {
ValidationError: {
message: data => `Validation failed for property "${data.field}".`,
properties: {
field: {
description: 'Field where validation failed.',
type: 'string'
},
value: {
description: 'value that was given.',
type: 'string'
}
},
required: [ 'field' ]
},
NestedError: {
message: 'error with nested example.',
properties: {
obj: {
type: 'object',
properties: {
value: {
description: 'value of a nested object',
type: 'string'
}
},
required: [ 'value' ]
}
},
required: [ 'obj' ]
},
NestedWithFunction: {
message: data => `Value: ${data.nested.value}`,
properties: {
nested: {
type: 'object',
properties: {
value: {
type: 'string',
minLength: 5
}
}
}
}
}
}
describe('createErrorDefinitions', function () {
context('with validation', function () {
const definitions = createErrorDefinitions(DEFINITIONS)
const ValidationError = definitions.ValidationError
const NestedError = definitions.NestedError
it('definitions created', function () {
assert.strictEqual(ValidationError.name, 'ValidationError')
assert.strictEqual(typeof ValidationError.message, 'function')
assert.strictEqual(typeof ValidationError.validate, 'function')
assert.deepStrictEqual(ValidationError.data, DEFINITIONS.ValidationError.data)
assert.deepStrictEqual(ValidationError.required, DEFINITIONS.ValidationError.required)
assert.strictEqual(NestedError.name, 'NestedError')
assert.strictEqual(typeof NestedError.message, 'string')
assert.strictEqual(typeof NestedError.validate, 'function')
assert.deepStrictEqual(NestedError.data, DEFINITIONS.NestedError.data)
assert.deepStrictEqual(NestedError.required, DEFINITIONS.NestedError.required)
})
it('error can not be created if `data` are invalid', () => {
try {
throw new StrictError(ValidationError)
} catch (err) {
let expectedMessage = 'ValidationError \'data\' are invalid:'
expectedMessage += '\n - data should have required property \'field\''
assert.strictEqual(err.message, expectedMessage)
}
try {
throw new StrictError(NestedError, { obj: {} })
} catch (err) {
let expectedMessage = 'NestedError \'data\' are invalid:'
expectedMessage += '\n - data.obj should have required property \'value\''
assert.strictEqual(err.message, expectedMessage)
}
})
it('ValidationError is created', function () {
const error = new StrictError(ValidationError, { field: 'text' })
assert.strictEqual(error.name, 'ValidationError')
assert.strictEqual(error.message, 'Validation failed for property "text".')
assert.deepStrictEqual(error.data, { field: 'text' })
assert.strictEqual(typeof error.toJSON, 'function')
assert.deepStrictEqual(error.toJSON(), {
name: 'ValidationError',
message: 'Validation failed for property "text".',
data: { field: 'text' }
})
assert(error.stack)
})
it('NestedError is created, properties are immutable', function () {
const error = new StrictError(NestedError, { obj: { value: 'test' } })
assert.throws(() => {
error.message = 'something'
}, /Cannot assign to read only/)
assert.throws(() => {
error.data.something = 'something'
}, /Cannot add property /)
})
})
context('without validation', function () {
const definitions = createErrorDefinitions(DEFINITIONS, { validate: false })
const ValidationError = definitions.ValidationError
it('no error is thrown when missing required data', function () {
assert.doesNotThrow(() => {
new StrictError(ValidationError)
})
})
})
it('errors to json', () => {
const definitions = createErrorDefinitions(DEFINITIONS, { validate: false })
const json = errorsToJson(definitions)
assert.deepStrictEqual(json, [
{
name: 'ValidationError',
message: 'Validation failed for property "hello world".',
properties: {
field: {
description: 'Field where validation failed.',
type: 'string'
},
value: {
description: 'value that was given.',
type: 'string'
}
},
required: [ 'field' ],
type: 'object'
},
{
name: 'NestedError',
message: 'error with nested example.',
properties: {
obj: {
type: 'object',
properties: {
value: {
description: 'value of a nested object',
type: 'string'
}
},
required: [ 'value' ]
}
},
required: [ 'obj' ],
type: 'object'
},
{
name: 'NestedWithFunction',
message: 'Value: hello world',
properties: {
nested: {
properties: {
value: {
type: 'string',
minLength: 5
}
},
type: 'object'
}
},
type: 'object'
}
])
})
})