-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
176 lines (148 loc) · 5.9 KB
/
test.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
'use strict'
var contextulizer = require('./index')
var assert = require('assert')
var express = require('express')
var request = require('supertest')
describe('index.js', function() {
describe('wrap', function() {
it(
'should return the passed in error if the passed error is falsy',
function() {
var out
out = contextulizer(null, 'whatever')
assert.strictEqual(out, null)
out = contextulizer(undefined, 'whatever')
assert.strictEqual(out, undefined)
}
)
it('should create a VError if passed an error', function() {
var e = new Error('inner error')
var wrapped_e = contextulizer(e)
assert(wrapped_e.cause())
})
it('should create a VError if passed an error and message', function() {
var e = new Error('inner error')
var desc = 'more description'
var wrapped_e = contextulizer(e, desc)
assert(wrapped_e.cause())
assert(wrapped_e.toString().indexOf(desc) > -1)
})
it('should create a VError if passed a string', function() {
var e = 'just a string'
var wrapped_e = contextulizer(e)
assert(wrapped_e instanceof Error)
})
it('should create a VError if passed an object', function() {
var e = { whatever: 'this is stupid' }
var wrapped_e = contextulizer(e)
assert(wrapped_e instanceof Error)
})
it('should create a VError if passed an array', function() {
var e = [{ whatever: 'this is stupid' }]
var wrapped_e = contextulizer(e)
assert(wrapped_e instanceof Error)
})
it('should create a VError if passed a number', function() {
var e = 2345
var wrapped_e = contextulizer(e)
assert(wrapped_e instanceof Error)
})
it('should work with a "error" that is a string containing a %',
function() {
var wrapped_e = contextulizer('%d wofijwef % % % sdfd%%%sadf%')
assert(wrapped_e instanceof Error)
})
it('should work for multiple levels of wrapping', function() {
var e = new Error('inner error')
var wrapped_e = contextulizer(e, 'first wrapper')
var wrapped_again = contextulizer(wrapped_e, 'second wrapper')
assert(wrapped_again.cause().cause())
var expected_desc = 'second wrapper: first wrapper: inner error'
assert.equal(wrapped_again.message, expected_desc)
})
describe('should return a function with a wrapped error handler if '
+ 'passed a function', function() {
it('passed errors should be wrapped', function() {
var old_callback = function(err, result) {
// console.log(err)
// console.log(err.stack)
assert(err.cause())
}
function async(callback) {
process.nextTick(function() {
callback(new Error('error in async'))
})
}
async(contextulizer(old_callback, 'error in this place'))
})
it('callback should work with if no error is passed', function() {
var old_callback = function(err, result) {
// console.log(err)
// console.log(result)
assert.equal(err, null)
assert.equal(result, 'good result')
}
function async(callback) {
process.nextTick(function() {
callback(null, 'good result')
})
}
async(contextulizer(old_callback, 'error info'))
})
})
it('should produce a stack containing each level', function(done) {
function async1(callback) {
process.nextTick(function() {
callback(new Error('error in async1'))
})
}
function async2(callback) {
var data1 = 'hi'
var data2 = 'buddy'
async1(function(err) {
callback(contextulizer(err), data1, data2)
})
}
function async3(callback) {
async2(function(err, data1, data2) {
assert.equal(data1, 'hi')
assert.equal(data2, 'buddy')
callback(contextulizer(err, 'error in async3'))
})
}
function async4(callback) {
async3(contextulizer(callback, 'error in async4'))
}
async4(function(err) {
// console.log(err.stack)
var expected_lines = 6
if (process.versions.node > '4') ++expected_lines
var lines = err.stack.split('\n').length
assert.equal(lines, expected_lines)
done()
})
})
it('should behave as expected when passed to next() in express',
function(done) {
var app = express();
// request handler
app.use(function(req, res, next){
var e = new Error('justa test error')
next(contextulizer(e, 'more context'))
})
// error middleware
app.use(function(err, req, res, next) {
assert(err instanceof Error)
// console.dir(err)
res.status(500).send(err)
})
request(app)
.get('/')
.expect(500)
.end(function(err, res){
// console.dir(res.error)
done()
});
})
})
})