/
index.js
1031 lines (869 loc) · 36.1 KB
/
index.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// jscs:disable maximumLineLength
'use strict';
// core modules
var http = require('http');
// userland
var assert = require('chai').assert;
var bunyan = require('bunyan');
var _ = require('lodash');
var restify = require('restify');
var restifyClients = require('restify-clients');
var nerror = require('@netflix/nerror');
var WError = nerror.WError;
var VError = nerror.VError;
var MultiError = nerror.MultiError;
// internal
var helpers = require('../lib/helpers');
var HttpError = require('../lib/baseClasses/HttpError');
var RestError = require('../lib/baseClasses/RestError');
var httpErrors = require('../lib/httpErrors');
var restErrors = require('../lib/restErrors');
var restifyErrors = require('../lib/index.js');
describe('restify-errors node module.', function() {
var ExecutionError;
describe('HttpError class', function() {
it('should create generic HttpError, inheriting from WError',
function() {
var myErr = new HttpError();
assert.equal(myErr instanceof HttpError, true);
assert.equal(myErr instanceof WError, true);
assert.equal(myErr instanceof Error, true);
assert.equal(myErr.code, 'Error');
assert.deepEqual(JSON.stringify(myErr), JSON.stringify({
code: 'Error',
message: ''
}));
});
it('should create HttpError using options object', function() {
var options = {
statusCode: 799,
code: 'myhttp',
info: {
foo: 'bar',
baz: [ 1, 2, 3 ]
}
};
var errMsg = 'my http error';
var myErr = new HttpError(options, errMsg);
// verify properties on the error
assert.equal(myErr.name, 'HttpError');
assert.equal(myErr.message, errMsg);
assert.equal(myErr.statusCode, options.statusCode);
assert.equal(myErr.code, 'myhttp');
assert.isObject(myErr.body);
assert.equal(myErr.body.message, errMsg);
assert.equal(myErr.body.code, 'myhttp');
assert.deepEqual(restifyErrors.info(myErr), options.info);
});
it('should create HttpError and retain a prior cause', function() {
// create http error with prior cause
var priorErr = new Error('foobar');
var myErr = new HttpError(priorErr, 'new message');
assert.equal(myErr.cause(), priorErr);
assert.equal(myErr.name, 'HttpError');
assert.equal(myErr.message, 'new message');
assert.isObject(myErr.body);
assert.equal(myErr.body.message, 'new message');
assert.equal(myErr.body.code, 'Error');
// create http error with prior cause and options
var myErr2Msg = 'bazbar';
var myErr2 = new HttpError({
cause: priorErr
}, myErr2Msg);
assert.equal(myErr2.cause(), priorErr);
assert.equal(myErr2.name, 'HttpError');
assert.equal(myErr2.message, myErr2Msg);
assert.isObject(myErr2.body);
assert.equal(myErr2.body.message, myErr2Msg);
assert.equal(myErr2.body.code, 'Error');
});
it('should create HttpError, args should fall through to WError',
function() {
var myErr = new HttpError('missing file: "%s"', 'foobar');
assert.equal(myErr.message, 'missing file: "foobar"');
});
it('should support .context property getter', function() {
var info = {
a: 1,
b: 2
};
var myErr = new HttpError({
info: info
}, 'boom');
assert.deepEqual(nerror.info(myErr), info);
assert.deepEqual(restifyErrors.info(myErr), info);
assert.deepEqual(myErr.context, info);
});
it('should override default toJSON and toString', function() {
var myErr = new HttpError({
statusCode: 999,
toString: function() {
return 'boom';
},
toJSON: function() {
var statusCode = this.statusCode;
return {
statusCode: statusCode
};
}
}, 'boom');
assert.strictEqual(myErr.toString(), 'boom');
assert.strictEqual(
JSON.stringify(myErr),
'{"statusCode":999}'
);
});
});
describe('Built-in HttpError subclasses', function() {
it('should create BadGatewayError, inheriting from HttpError from ' +
'WError', function() {
var myErr = new httpErrors.BadGatewayError();
assert.equal(myErr instanceof httpErrors.BadGatewayError, true);
assert.equal(myErr instanceof HttpError, true);
assert.equal(myErr instanceof WError, true);
assert.equal(myErr instanceof Error, true);
// assert default status code
assert.equal(myErr.code, 'BadGateway');
assert.equal(myErr.statusCode, 502);
assert.equal(myErr.message, '');
// assert stringification
assert.equal(JSON.stringify(myErr), JSON.stringify({
code: 'BadGateway',
message: ''
}));
});
it('should create BadGatewayError using options object', function() {
var msg = 'my http error';
var myErr = new httpErrors.BadGatewayError({
statusCode: 799, // can pass in any crazy status code
code: 'myhttp',
info: {
foo: 'bar',
baz: [ 1, 2, 3 ]
}
}, msg);
assert.equal(myErr.name, 'BadGatewayError');
assert.equal(myErr.message, msg);
assert.equal(myErr.statusCode, 799);
assert.equal(myErr.code, 'myhttp');
assert.isObject(myErr.body);
assert.equal(myErr.body.message, msg);
assert.equal(myErr.body.code, 'myhttp');
assert.deepEqual(restifyErrors.info(myErr), {
foo: 'bar',
baz: [ 1, 2, 3 ]
});
});
it('should create BadGatewayError, and retain a prior cause',
function() {
var priorErr = new Error('foobar');
var myErr = new httpErrors.BadGatewayError(priorErr);
assert.equal(myErr.cause(), priorErr);
assert.equal(myErr.name, 'BadGatewayError');
assert.equal(myErr.statusCode, 502);
assert.isObject(myErr.body);
assert.equal(myErr.body.message, '');
assert.equal(myErr.body.code, 'BadGateway');
var myErr2Msg = 'bazbar';
var myErr2 = new httpErrors.BadGatewayError({
cause: priorErr
}, myErr2Msg);
assert.equal(myErr.cause(), priorErr);
assert.equal(myErr2.name, 'BadGatewayError');
assert.equal(myErr2.statusCode, 502);
assert.equal(myErr2.message, myErr2Msg);
assert.isObject(myErr2.body);
assert.equal(myErr2.body.message, myErr2Msg);
assert.equal(myErr2.body.code, 'BadGateway');
});
it('should create BadGatewayError, args should fall through to WError',
function() {
var myErr = new httpErrors.BadGatewayError(
'missing file: "%s"', 'foobar'
);
assert.equal(myErr.message, 'missing file: "foobar"');
});
});
describe('RestError class', function() {
it('should create generic RestError, inheriting from WError',
function() {
var myErr = new RestError();
assert.equal(myErr instanceof RestError, true);
assert.equal(myErr instanceof WError, true);
assert.equal(myErr instanceof Error, true);
assert.equal(myErr.code, 'Error');
assert.equal(myErr.restCode, 'Error');
// assert stringification
assert.equal(JSON.stringify(myErr), JSON.stringify({
code: 'Error',
message: ''
}));
});
it('should create RestError, using options object', function() {
var errMsg = 'my http error';
var options = {
statusCode: 799,
info: {
foo: 'bar',
baz: [ 1, 2, 3 ]
}
};
var myErr = new RestError(options, errMsg);
// verify properties on the error
assert.equal(myErr.name, 'RestError');
assert.equal(myErr.restCode, 'Error');
assert.equal(myErr.message, errMsg);
assert.equal(myErr.statusCode, options.statusCode);
assert.isObject(myErr.body);
assert.equal(myErr.body.message, errMsg);
assert.equal(myErr.body.code, 'Error');
assert.deepEqual(restifyErrors.info(myErr), {
foo: 'bar',
baz: [ 1, 2, 3 ]
});
});
it('should create RestError, and retain a prior cause', function() {
// create http error with prior cause
var priorErr = new Error('foobar');
var myErr = new RestError(priorErr);
assert.equal(myErr.cause(), priorErr);
assert.equal(myErr.name, 'RestError');
assert.equal(myErr.restCode, 'Error');
assert.equal(myErr.message, '');
assert.isObject(myErr.body);
assert.equal(myErr.body.message, '');
assert.equal(myErr.body.code, 'Error');
// create http error with prior cause and options
var errMsg = 'bazbar';
var options = {
cause: priorErr,
restCode: 'yay'
};
var myErr2 = new RestError(options, errMsg);
assert.equal(myErr2.cause(), priorErr);
assert.equal(myErr2.name, 'RestError');
assert.equal(myErr2.restCode, options.restCode);
assert.equal(myErr2.message, errMsg);
assert.isObject(myErr2.body);
assert.equal(myErr2.body.message, errMsg);
assert.equal(myErr2.body.code, 'yay');
});
it('should create RestError, args should fall through to WError',
function() {
var myErr = new RestError('missing file: "%s"', 'foobar');
assert.equal(myErr.message, 'missing file: "foobar"');
});
});
describe('Built-in RestError subclasses', function() {
it('should create BadDigestError, inheriting from RestError' +
'/HttpError/WError', function() {
var myErr = new restErrors.BadDigestError();
assert.equal(myErr instanceof restErrors.BadDigestError, true);
assert.equal(myErr instanceof RestError, true);
assert.equal(myErr instanceof HttpError, true);
assert.equal(myErr instanceof WError, true);
assert.equal(myErr instanceof Error, true);
assert.equal(myErr.code, 'Error');
assert.equal(myErr.restCode, 'BadDigest');
// assert stringification
assert.equal(JSON.stringify(myErr), JSON.stringify({
code: 'BadDigest',
message: ''
}));
});
it('should create BadDigestError, using options object', function() {
var options = {
restCode: 'yay',
statusCode: 799,
info: {
foo: 'bar',
baz: [ 1, 2, 3 ]
}
};
var errMsg = 'my http error';
var myErr = new restErrors.BadDigestError(options, errMsg);
// verify properties on the error
assert.equal(myErr.name, 'BadDigestError');
assert.equal(myErr.restCode, options.restCode);
assert.equal(myErr.message, errMsg);
assert.equal(myErr.statusCode, options.statusCode);
assert.isObject(myErr.body);
assert.equal(myErr.body.message, errMsg);
assert.equal(myErr.body.code, 'yay');
assert.deepEqual(restifyErrors.info(myErr), {
foo: 'bar',
baz: [ 1, 2, 3 ]
});
});
it('should create BadDigestError, args should fall through to WError',
function() {
var myErr = new restErrors.BadDigestError(
'missing file: "%s"', 'foobar'
);
assert.equal(myErr.name, 'BadDigestError');
assert.equal(myErr.restCode, 'BadDigest');
assert.equal(myErr.statusCode, 400);
assert.isObject(myErr.body);
assert.equal(myErr.body.message, 'missing file: "foobar"');
assert.equal(myErr.body.code, 'BadDigest');
});
it('should create BadDigestError using options, should prefer ' +
'printf over options', function() {
var myErr = new restErrors.BadDigestError({
restCode: 'Bad Digestion',
message: 'this error should not match'
}, 'missing file: "%s"', 'foobar');
assert.equal(myErr.name, 'BadDigestError');
assert.equal(myErr.restCode, 'Bad Digestion');
assert.equal(myErr.statusCode, 400);
assert.isObject(myErr.body);
assert.equal(myErr.body.message, 'missing file: "foobar"');
assert.equal(myErr.body.code, 'Bad Digestion');
});
});
describe('helpers', function() {
function parse() {
return helpers.parseVErrorArgs(arguments);
}
it('should parse VError arguments with options object', function() {
var options = {
statusCode: 101
};
var parsed = parse(options);
assert.deepEqual(parsed, {
verrorArgs: [{}],
internalOpts: {
statusCode: 101
}
});
});
it('should parse VError arguments with strings (pass through to ' +
'WError)', function() {
var parsed = parse('missing file: "%s"', 'foobar');
assert.deepEqual(parsed, {
verrorArgs: [ 'missing file: "%s"', 'foobar' ],
internalOpts: {}
});
});
it('should parse variadic arguments with priorCause and strings ' +
'(pass through to WError)', function() {
var err = new Error('foobar');
var parsed = parse(err, 'a', 'b', 'c');
assert.deepEqual(parsed, {
verrorArgs: [ err, 'a', 'b', 'c' ],
internalOpts: {}
});
});
});
describe('stack trace cleanliness', function() {
it('should have test file as first line of HttpError stack trace',
function testStack1() {
var httpErr = new HttpError('http error');
var stack = httpErr.stack.split('\n');
// ensure stack trace's first line is the test file.
assert.equal(_.includes(stack[0], 'HttpError: http error'), true);
assert.equal(_.includes(stack[1], 'Context.testStack1'), true);
assert.equal(_.includes(stack[1], 'test/index.js'), true);
});
it('should have test file as first line of built-in HttpError ' +
'stack trace', function testStack2() {
// test built in http errors
var badReqErr = new httpErrors.BadRequestError('i am bad');
var stack = badReqErr.stack.split('\n');
assert.equal(
_.includes(stack[0], 'BadRequestError: i am bad'),
true
);
assert.equal(_.includes(stack[1], 'Context.testStack2'), true);
assert.equal(_.includes(stack[1], 'test/index.js'), true);
});
it('should have test file as first line in RestError stack trace',
function testStack3() {
// test built in http errors
var restErr = new RestError('i am rest');
var stack = restErr.stack.split('\n');
assert.equal(_.includes(stack[0], 'RestError: i am rest'), true);
assert.equal(_.includes(stack[1], 'Context.testStack3'), true);
assert.equal(_.includes(stack[1], 'test/index.js'), true);
});
it('should have test file as first line of built-in RestError stack ' +
'trace', function testStack4() {
// test built in http errors
var badDigestError = new restErrors.BadDigestError('indigestion');
var stack = badDigestError.stack.split('\n');
assert.equal(
_.includes(stack[0], 'BadDigestError: indigestion'),
true
);
assert.equal(_.includes(stack[1], 'Context.testStack4'), true);
assert.equal(_.includes(stack[1], 'test/index.js'), true);
});
it('should have test file as first line of subclass error stack trace',
function testStack5() {
var ChargeError = restifyErrors.makeConstructor('ChargeError');
var err = new ChargeError('did not charge long enough');
var stack = err.stack.split('\n');
assert.equal(
_.includes(stack[0], 'ChargeError: did not charge long enough'),
true
);
assert.equal(_.includes(stack[1], 'Context.testStack5'), true);
assert.equal(_.includes(stack[1], 'test/index.js'), true);
});
it('should have test file as first line of stack trace for error ' +
'created via makeErrFromCode', function testStack6() {
var err = restifyErrors.makeErrFromCode(401, 'no creds');
var stack = err.stack.split('\n');
assert.equal(
_.includes(stack[0], 'UnauthorizedError: no creds'),
true
);
assert.equal(_.includes(stack[1], 'Context.testStack6'), true);
assert.equal(_.includes(stack[1], 'test/index.js'), true);
});
});
describe('main exports', function() {
it('should export a constructor for every http error code (400-500)',
function() {
// ensure we have the same amount of errors for every http error
// exposed on the core http module. we only care about http status
// codes 400-500.
var numRawErrors = _.filter(
http.STATUS_CODES,
function(desc, code) {
return parseInt(code, 10) >= 400;
}
).length;
assert.equal(
numRawErrors,
_.size(httpErrors)
);
// ensure each one has a displayName that ends in 'Error'
// then try to new up one of each.
_.forEach(httpErrors, function(ErrCtor) {
assert.isString(ErrCtor.displayName);
assert.equal(_.endsWith(ErrCtor.displayName, 'Error'), true);
var err = new ErrCtor();
assert.equal(err instanceof Error, true);
});
});
it('should export a constructor for every built-in RestError type',
function() {
// no good way to verify we got all the constructors, so it's hard
// coded for now.
// 16 built-in RestError subclasses
assert.equal(_.size(restErrors), 15);
// ensure each one has a displayName that ends in 'Error'
// then try to new up one of each.
_.forEach(httpErrors, function(ErrCtor) {
assert.isString(ErrCtor.displayName);
assert.equal(_.endsWith(ErrCtor.displayName, 'Error'), true);
var err = new ErrCtor();
assert.equal(err instanceof Error, true);
});
});
it('should export roll up of all constructors', function() {
assert.isObject(restifyErrors);
// again, no way to know since we programatically create errors,
// but ensure at least we have all RestErrors
assert.isAbove(_.size(restifyErrors), 30);
});
it('should have code properties for all HttpError constructors',
function() {
_.forEach(httpErrors, function(HttpErr) {
var err = new HttpErr();
// strip off the last 5 chars ('Error') and do an assertion
assert.equal(err.code, HttpErr.displayName.slice(0, -5));
});
});
it('should have restCode properties for all RestError constructors',
function() {
_.forEach(restErrors, function(RestErr) {
var err = new RestErr();
// strip off the last 5 chars ('Error') and do an assertion
assert.equal(err.restCode, RestErr.displayName.slice(0, -5));
});
});
it('should create custom error using makeConstructor', function() {
ExecutionError = restifyErrors.makeConstructor('ExecutionError', {
statusCode: 406,
failureType: 'motion',
code: 'moo',
message: 'Default Execution Error'
});
});
it('should create custom error instance', function() {
var underlyingErr = new Error('underlying error!');
var err = new ExecutionError(underlyingErr, 'bad joystick input');
assert.equal(err instanceof ExecutionError, true);
assert.equal(err instanceof RestError, true);
assert.equal(err instanceof HttpError, true);
assert.equal(err instanceof WError, true);
assert.equal(err instanceof Error, true);
assert.equal(err.message, 'bad joystick input');
assert.equal(err.statusCode, 406);
assert.equal(err.failureType, 'motion');
assert.equal(err.restCode, 'Execution');
assert.equal(err.code, 'moo');
assert.isObject(err.body);
assert.equal(err.body.code, 'Execution');
assert.equal(err.body.message, 'bad joystick input');
assert.equal(err.cause(), underlyingErr);
// assert stringification
var expectedJSON = {
code: 'Execution',
message: 'bad joystick input; ' +
'caused by Error: underlying error!'
};
assert.equal(JSON.stringify(err), JSON.stringify(expectedJSON));
assert.equal(
err.toString(),
err.name + ': ' + expectedJSON.message
);
});
it('should create custom error instance using options', function() {
var underlyingErr = new Error('underlying error!');
var options = {
cause: underlyingErr,
statusCode: 799,
info: {
foo: 'bar',
baz: [ 1, 2, 3 ]
}
};
var errMsg = 'bad joystick input';
var err = new ExecutionError(options, errMsg);
assert.equal(err instanceof ExecutionError, true);
assert.equal(err instanceof RestError, true);
assert.equal(err instanceof HttpError, true);
assert.equal(err instanceof WError, true);
assert.equal(err instanceof Error, true);
assert.equal(err.message, 'bad joystick input');
assert.equal(err.statusCode, 799);
assert.equal(err.failureType, 'motion');
assert.equal(err.restCode, 'Execution');
assert.equal(err.code, 'moo');
assert.isObject(err.body);
assert.equal(err.body.code, 'Execution');
assert.equal(err.body.message, 'bad joystick input');
assert.equal(err.cause(), underlyingErr);
assert.deepEqual(restifyErrors.info(err), {
foo: 'bar',
baz: [ 1, 2, 3 ]
});
// assert stringification
var expectedJSON = {
code: 'Execution',
message: 'bad joystick input; ' +
'caused by Error: underlying error!'
};
assert.equal(JSON.stringify(err), JSON.stringify(expectedJSON));
assert.equal(
err.toString(),
err.name + ': ' + expectedJSON.message
);
});
it('should create custom error using makeConstructor (with lower ' +
'case Error name)', function() {
var underlyingErr = new Error('underlying error!');
var Executionerror = restifyErrors.makeConstructor(
'Executionerror',
{
statusCode: 406,
failureType: 'motion',
code: 'moo'
}
);
var err = new Executionerror(underlyingErr, 'bad joystick input');
assert.equal(err instanceof Executionerror, true);
// assert stringification
var expectedJSON = {
code: 'Execution',
message: 'bad joystick input; ' +
'caused by Error: underlying error!'
};
assert.equal(JSON.stringify(err), JSON.stringify(expectedJSON));
assert.equal(
err.toString(),
err.name + ': ' + expectedJSON.message
);
});
it('should have message property fallback to custom error options',
function() {
var err = new ExecutionError('printf-style %s', 'error');
assert.equal(err.message, 'printf-style error');
err = new ExecutionError({
info: {
foo: 'bar'
}
}, 'printf-style %s', 'error');
assert.equal(err.message, 'printf-style error');
assert.deepEqual(restifyErrors.info(err), {
foo: 'bar'
});
err = new ExecutionError();
assert.equal(err.message, 'Default Execution Error');
// assert fallback to empty string if no message provided
var NoDefaultMessageError = restifyErrors.makeConstructor(
'NoDefaultMessageError'
);
err = new NoDefaultMessageError();
assert.equal(err.message, '');
});
it('should create an error from an http status code', function() {
var err = restifyErrors.makeErrFromCode(406, 'the horror');
assert.equal(
err instanceof restifyErrors.NotAcceptableError,
true
);
assert.equal(err instanceof HttpError, true);
assert.equal(err instanceof WError, true);
assert.equal(err instanceof Error, true);
assert.equal(err.message, 'the horror');
assert.equal(err.statusCode, 406);
assert.isObject(err.body);
assert.equal(err.body.code, 'NotAcceptable');
assert.equal(err.body.message, 'the horror');
// assert stringification
assert.equal(JSON.stringify(err), JSON.stringify({
code: 'NotAcceptable',
message: 'the horror'
}));
});
});
describe('restify integration', function() {
var server;
var client;
before(function(done) {
server = restify.createServer({
name: 'restifyErrors'
});
client = restifyClients.createJSONClient({
url: 'http://localhost:3000'
});
server.listen(3000, done);
});
after(function(done) {
client.close();
server.close(done);
});
it('should send HttpErrors with status codes', function(done) {
server.get('/1', function(req, res, next) {
res.send(new restifyErrors.NotFoundError('gone girl'));
next();
});
client.get('/1', function(err, req, res, data) {
assert.ok(err);
assert.equal(res.statusCode, 404);
assert.equal(data.message, 'gone girl');
done();
});
});
it('should send RestErrors with status codes', function(done) {
server.get('/2', function(req, res, next) {
res.send(new restifyErrors.BadDigestError('indigestion'));
next();
});
client.get('/2', function(err, req, res, data) {
assert.ok(err);
assert.equal(res.statusCode, 400);
assert.equal(data.message, 'indigestion');
done();
});
});
it('should send custom errors with status codes', function(done) {
server.get('/3', function(req, res, next) {
res.send(new ExecutionError('bad joystick input!'));
next();
});
client.get('/3', function(err, req, res, data) {
assert.ok(err);
assert.equal(res.statusCode, 406);
assert.equal(data.message, 'bad joystick input!');
done();
});
});
});
describe('bunyan serializer', function() {
var logger;
it('should expose default serializer function', function() {
var serializer = restifyErrors.bunyanSerializer;
assert.isFunction(serializer);
});
it('should expose factory for serializer creation', function() {
var create = restifyErrors.bunyanSerializer.create;
assert.isFunction(create);
});
it('should create a custom bucket of err serializer', function() {
// the factory returns a POJO of serializer keys to serializer
// functions.
var serializer = restifyErrors.bunyanSerializer.create();
// returned serializer object is of form:
// {
// err: <function>
// }
// which matches bunyan and pino
assert.isObject(serializer);
assert.deepEqual(_.keys(serializer), [ 'err' ]);
assert.isFunction(serializer.err);
});
it('should create bunyan logger with serializer', function() {
logger = bunyan.createLogger({
name: 'unit-test',
serializers: {
err: restifyErrors.bunyanSerializer
}
});
});
it('should serialize a standard Error', function(done) {
var err = new Error('boom');
assert.doesNotThrow(function() {
logger.error(err, 'standard error');
});
done();
});
it('should ignore serializer', function(done) {
// pass an error object without stack
assert.doesNotThrow(function() {
logger.error({
err: null
}, 'wrapped error');
});
assert.doesNotThrow(function() {
logger.error({
err: {}
}, 'wrapped error');
});
done();
});
it('should handle circular refs', function(done) {
var a = {};
var b = { foo: a };
a.foo = b;
var err = new RestError({
message: 'boom',
info: a
});
assert.doesNotThrow(function() {
logger.error({
err: err
}, 'wrapped error');
});
done();
});
it('should serialize a VError with info', function() {
var err = new VError({
name: 'VErrorInfo',
info: {
foo: 'qux',
baz: 2
}
}, 'this is a verror with info');
assert.doesNotThrow(function() {
logger.error(err);
});
});
it('should serialize a MultiError', function() {
var err1 = new Error('boom');
var err2 = new restifyErrors.InternalServerError({
cause: err1,
info: {
foo: 'bar',
baz: 1
}
}, 'ISE');
var err3 = new VError({
name: 'VErrorInfo',
cause: err1,
info: {
foo: 'qux',
baz: 2
}
}, 'this is a verror with info');
var multiError = new MultiError([ err1, err2, err3 ]);
assert.doesNotThrow(function() {
logger.error(multiError, 'MultiError');
});
});
it('should not serialize arbitrary fields on VError', function() {
var err1 = new VError({
name: 'VErrorInfo',
info: {
espresso: 'ristretto'
}
}, 'pull!');
err1.espresso = 'lungo';
assert.doesNotThrow(function() {
logger.error(err1, 'verror with fields');
});
});
it('should not serialize arbitrary fields on Error', function() {
var serializer = restifyErrors.bunyanSerializer;
var err1 = new Error('pull!');
err1.espresso = 'normale';
var serializedErr = serializer(err1);
assert.notInclude(serializedErr.stack, 'espresso=normale');
});
it('should serialize arbitrary fields on Error', function() {
var serializer = restifyErrors.bunyanSerializer.create({
topLevelFields: true
});
var err1 = new Error('pull!');
err1.espresso = 'normale';
logger.child({ serializers: serializer }).error(err1);
var serializedErr = serializer.err(err1, 'oh noes!');
assert.notInclude(serializedErr.stack, 'espresso=normale');
});
it('should not serialize known fields on VError', function() {
var serializer = restifyErrors.bunyanSerializer.create({
topLevelFields: true
});
var err1 = new VError({
name: 'VErrorInfo',
info: {
espresso: 'ristretto'
}
}, 'pull!');
err1.espresso = 'lungo';
logger.child({ serializers: serializer }).error(err1);
var serializedErr = serializer.err(err1, 'oh noes!');
assert.notInclude(serializedErr.stack, 'cause=undefined');
});
it('should serialize domain when not using node domains', function() {
var serializer = restifyErrors.bunyanSerializer.create({
topLevelFields: true
});
var err1 = new Error('foo');
err1.domain = 'bar';
logger.child({ serializers: serializer }).error(err1);
var serializedErr = serializer.err(err1, 'oh noes!');
assert.include(serializedErr.stack, 'domain="bar"');
});