parsers.js
4.61 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
'use strict'
var test = require('tape')
var semver = require('semver')
var parsers = require('../lib/parsers')
test('#parseMessage()', function (t) {
t.test('should parse string', function (t) {
var data = {}
parsers.parseMessage('Howdy', data)
t.equal(data.message, 'Howdy')
t.end()
})
t.test('should parse object', function (t) {
var data = {}
parsers.parseMessage({ message: 'foo%s', params: ['bar'] }, data)
t.equal(data.message, 'foobar')
t.equal(data.param_message, 'foo%s')
t.end()
})
t.test('should parse an invalid object', function (t) {
var data = {}
parsers.parseMessage({ foo: /bar/ }, data)
t.equal(data.message, '{ foo: /bar/ }')
t.end()
})
})
test('#parseRequest()', function (t) {
var mockReq = {
method: 'GET',
url: '/some/path?key=value',
headers: {
host: 'example.com'
},
body: '',
cookies: {},
socket: {
encrypted: true
}
}
t.test('should parse a request object', function () {
var parsed = parsers.parseRequest(mockReq)
t.equal(parsed.url, 'https://example.com/some/path?key=value')
t.end()
})
t.test('should slice too large body\'s', function (t) {
mockReq.body = ''
for (var n = 0; n < parsers._MAX_HTTP_BODY_CHARS + 10; n++) {
mockReq.body += 'x'
}
var parsed = parsers.parseRequest(mockReq)
t.equal(parsed.data.length, parsers._MAX_HTTP_BODY_CHARS)
t.end()
})
})
test('#parseError()', function (t) {
t.test('should parse plain Error object', function (t) {
parsers.parseError(new Error(), {}, function (parsed) {
t.equal(parsed.message, 'Error: <no message>')
t.ok('exception' in parsed)
t.equal(parsed.exception.type, 'Error')
t.equal(parsed.exception.value, '')
t.ok('stacktrace' in parsed)
t.ok('frames' in parsed.stacktrace)
t.end()
})
})
t.test('should parse Error with message', function (t) {
parsers.parseError(new Error('Crap'), {}, function (parsed) {
t.equal(parsed.message, 'Error: Crap')
t.ok('exception' in parsed)
t.equal(parsed.exception.type, 'Error')
t.equal(parsed.exception.value, 'Crap')
t.ok('stacktrace' in parsed)
t.ok('frames' in parsed.stacktrace)
t.end()
})
})
t.test('should parse TypeError with message', function (t) {
parsers.parseError(new TypeError('Crap'), {}, function (parsed) {
t.equal(parsed.message, 'TypeError: Crap')
t.ok('exception' in parsed)
t.equal(parsed.exception.type, 'TypeError')
t.equal(parsed.exception.value, 'Crap')
t.ok('stacktrace' in parsed)
t.ok('frames' in parsed.stacktrace)
t.end()
})
})
t.test('should parse thrown Error', function (t) {
try {
throw new Error('Derp')
} catch (e) {
parsers.parseError(e, {}, function (parsed) {
t.equal(parsed.message, 'Error: Derp')
t.ok('exception' in parsed)
t.equal(parsed.exception.type, 'Error')
t.equal(parsed.exception.value, 'Derp')
t.ok('stacktrace' in parsed)
t.ok('frames' in parsed.stacktrace)
t.end()
})
}
})
t.test('should parse caught real error', function (t) {
try {
var o = {}
o['...']['Derp']()
} catch (e) {
parsers.parseError(e, {}, function (parsed) {
var msg = semver.lt(process.version, '0.11.0')
? 'Cannot call method \'Derp\' of undefined'
: 'Cannot read property \'Derp\' of undefined'
t.equal(parsed.message, 'TypeError: ' + msg)
t.ok('exception' in parsed)
t.equal(parsed.exception.type, 'TypeError')
t.equal(parsed.exception.value, msg)
t.ok('stacktrace' in parsed)
t.ok('frames' in parsed.stacktrace)
t.end()
})
}
})
t.test('should gracefully handle .stack already being accessed', function (t) {
var err = new Error('foo')
t.ok(typeof err.stack === 'string')
parsers.parseError(err, {}, function (parsed) {
t.equal(parsed.message, 'Error: foo')
t.ok('exception' in parsed)
t.equal(parsed.exception.type, 'Error')
t.equal(parsed.exception.value, 'foo')
t.ok('stacktrace' in parsed)
t.ok('frames' in parsed.stacktrace)
t.end()
})
})
t.test('should gracefully handle .stack being overwritten', function (t) {
var err = new Error('foo')
err.stack = 'foo'
parsers.parseError(err, {}, function (parsed) {
t.equal(parsed.message, 'Error: foo')
t.ok('exception' in parsed)
t.equal(parsed.exception.type, 'Error')
t.equal(parsed.exception.value, 'foo')
t.ok(!('stacktrace' in parsed))
t.end()
})
})
})