-
-
Notifications
You must be signed in to change notification settings - Fork 798
Expand file tree
/
Copy patherror.test.js
More file actions
315 lines (267 loc) · 7.79 KB
/
error.test.js
File metadata and controls
315 lines (267 loc) · 7.79 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
const assert = require('assert');
const feathers = require('../../lib');
describe('`error` hooks', () => {
describe('on direct service method errors', () => {
const errorMessage = 'Something else went wrong';
const app = feathers().use('/dummy', {
get () {
return Promise.reject(new Error('Something went wrong'));
}
});
const service = app.service('dummy');
afterEach(() => service.__hooks.error.get = []);
it('basic error hook', () => {
service.hooks({
error: {
get (hook) {
assert.strictEqual(hook.type, 'error');
assert.strictEqual(hook.id, 'test');
assert.strictEqual(hook.method, 'get');
assert.strictEqual(hook.app, app);
assert.strictEqual(hook.error.message, 'Something went wrong');
}
}
});
return service.get('test').then(() => {
throw new Error('Should never get here');
}).catch(() => true);
});
it('can change the error', () => {
service.hooks({
error: {
get (hook) {
hook.error = new Error(errorMessage);
}
}
});
return service.get('test').catch(error => {
assert.strictEqual(error.message, errorMessage);
});
});
it('throwing an error', () => {
service.hooks({
error: {
get () {
throw new Error(errorMessage);
}
}
});
return service.get('test').catch(error => {
assert.strictEqual(error.message, errorMessage);
});
});
it('rejecting a promise', () => {
service.hooks({
error: {
get () {
return Promise.reject(new Error(errorMessage));
}
}
});
return service.get('test').catch(error => {
assert.strictEqual(error.message, errorMessage);
});
});
it('calling `next` with error', () => {
service.hooks({
error: {
get () {
throw new Error(errorMessage);
}
}
});
return service.get('test').catch(error => {
assert.strictEqual(error.message, errorMessage);
});
});
it('can chain multiple hooks', () => {
service.hooks({
error: {
get: [
function (hook) {
hook.error = new Error(errorMessage);
hook.error.first = true;
},
function (hook) {
hook.error.second = true;
return Promise.resolve(hook);
},
function (hook) {
hook.error.third = true;
return hook;
}
]
}
});
return service.get('test').catch(error => {
assert.strictEqual(error.message, errorMessage);
assert.ok(error.first);
assert.ok(error.second);
assert.ok(error.third);
});
});
it('setting `hook.result` will return result', () => {
const data = {
message: 'It worked'
};
service.hooks({
error: {
get (hook) {
hook.result = data;
}
}
});
return service.get(10)
.then(result => assert.deepStrictEqual(result, data));
});
it('allows to set `context.result = null` in error hooks (#865)', () => {
const app = feathers().use('/dummy', {
get () {
return Promise.reject(new Error('Damnit'));
}
});
app.service('dummy').hooks({
error: {
get (context) {
context.result = null;
}
}
});
return app.service('dummy').get(1)
.then(result => assert.strictEqual(result, null));
});
it('uses the current hook object if thrown in a service method', () => {
const app = feathers().use('/dummy', {
get () {
return Promise.reject(new Error('Something went wrong'));
}
});
const service = app.service('dummy');
service.hooks({
before (hook) {
return { ...hook, id: 42 };
},
error (hook) {
assert.strictEqual(hook.id, 42);
}
});
return service.get(1).then(
() => assert.fail('Should never get here'),
error => assert.strictEqual(error.message, 'Something went wrong')
);
});
});
describe('error in hooks', () => {
const errorMessage = 'before hook broke';
let app;
let service;
beforeEach(() => {
app = feathers().use('/dummy', {
get (id) {
return Promise.resolve({
id, text: `You have to do ${id}`
});
}
});
service = app.service('dummy');
});
it('in before hook', () => {
service.hooks({
before () {
throw new Error(errorMessage);
}
}).hooks({
error (hook) {
assert.strictEqual(hook.error.hook.type, 'before',
'Original hook still set'
);
assert.strictEqual(hook.id, 'dishes');
assert.strictEqual(hook.error.message, errorMessage);
}
});
return service.get('dishes')
.then(() => {
throw new Error('Should never get here');
})
.catch(error => assert.strictEqual(error.message, errorMessage));
});
it('in after hook', () => {
service.hooks({
after () {
throw new Error(errorMessage);
},
error (hook) {
assert.strictEqual(hook.error.hook.type, 'after',
'Original hook still set'
);
assert.strictEqual(hook.id, 'dishes');
assert.deepStrictEqual(hook.original.result, {
id: 'dishes',
text: 'You have to do dishes'
});
assert.strictEqual(hook.error.message, errorMessage);
}
});
return service.get('dishes')
.then(() => {
throw new Error('Should never get here');
})
.catch(error => assert.strictEqual(error.message, errorMessage));
});
it('uses the current hook object if thrown in a hook and sets hook.original', () => {
service.hooks({
after (hook) {
hook.modified = true;
throw new Error(errorMessage);
},
error (hook) {
assert.ok(hook.modified);
assert.strictEqual(hook.original.type, 'after');
}
});
return service.get('laundry')
.then(() => {
throw new Error('Should never get here');
})
.catch(error => assert.strictEqual(error.message, errorMessage));
});
});
it('Error in before hook causes inter-service calls to have wrong hook context (https://github.com/feathersjs/feathers/issues/841)', () => {
const app = feathers();
let service1Params;
let service2Params;
app.use('/service1', {
find () {
return Promise.resolve({ message: 'service1 success' });
}
});
app.service('service1').hooks({
before (context) {
service1Params = context.params;
return Promise.reject(new Error('Error in service1 before hook'));
}
});
app.use('/service2', {
find (params) {
return app.service('/service1').find({}).then(() => {
return { message: 'service2 success' };
});
}
});
app.service('service2').hooks({
before (context) {
service2Params = context.params;
context.params.foo = 'bar';
},
error (context) {
assert.ok(service1Params !== context.params);
assert.ok(service2Params === context.params);
assert.strictEqual(context.path, 'service2');
assert.strictEqual(context.params.foo, 'bar');
}
});
return app.service('/service2').find().catch(error => {
assert.strictEqual(error.message, 'Error in service1 before hook');
});
});
});