-
Notifications
You must be signed in to change notification settings - Fork 39
/
errors.js
280 lines (259 loc) · 7.31 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
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
'use strict'
const npa = require('npm-package-arg')
const t = require('tap')
const tnock = require('./util/tnock.js')
const errors = require('../lib/errors.js')
const fetch = require('..')
const OPTS = {
timeout: 0,
retry: {
retries: 1,
factor: 1,
minTimeout: 1,
maxTimeout: 10,
},
registry: 'https://mock.reg/',
}
t.test('generic request errors', t => {
tnock(t, OPTS.registry)
.get('/ohno/oops')
.reply(400, 'failwhale!')
// verify that the otpPrompt won't save from non-OTP errors
const otpPrompt = () => {
throw new Error('nope')
}
return fetch('/ohno/oops', { ...OPTS, otpPrompt })
.then(
() => {
throw new Error('should not have succeeded!')
},
err => {
t.equal(
err.message,
`400 Bad Request - GET ${OPTS.registry}ohno/oops`,
'neatly printed message'
)
t.equal(err.code, 'E400', 'HTTP code used for err.code')
t.equal(err.statusCode, 400, 'numerical HTTP code available')
t.equal(err.method, 'GET', 'method in error object')
t.equal(err.body.toString('utf8'), 'failwhale!', 'req body reported')
t.equal(err.pkgid, 'oops', 'base path used for pkgid')
}
)
})
t.test('pkgid tie fighter', t => {
tnock(t, OPTS.registry)
.get('/-/ohno/_rewrite/ohyeah/maybe')
.reply(400, 'failwhale!')
return fetch('/-/ohno/_rewrite/ohyeah/maybe', OPTS)
.then(
() => {
throw new Error('should not have succeeded!')
},
err => t.equal(err.pkgid, undefined, 'no pkgid on tie fighters')
)
})
t.test('pkgid _rewrite', t => {
tnock(t, OPTS.registry)
.get('/ohno/_rewrite/ohyeah/maybe')
.reply(400, 'failwhale!')
return fetch('/ohno/_rewrite/ohyeah/maybe', OPTS)
.then(
() => {
throw new Error('should not have succeeded!')
},
err => t.equal(err.pkgid, 'ohyeah', '_rewrite filtered for pkgid')
)
})
t.test('pkgid with `opts.spec`', t => {
tnock(t, OPTS.registry)
.get('/ohno/_rewrite/ohyeah')
.reply(400, 'failwhale!')
return fetch('/ohno/_rewrite/ohyeah', {
...OPTS,
spec: npa('foo@1.2.3'),
})
.then(
() => {
throw new Error('should not have succeeded!')
},
err => t.equal(err.pkgid, 'foo@1.2.3', 'opts.spec used for pkgid')
)
})
t.test('JSON error reporing', t => {
tnock(t, OPTS.registry)
.get('/ohno')
.reply(400, { error: 'badarg' })
return fetch('/ohno', OPTS)
.then(
() => {
throw new Error('should not have succeeded!')
},
err => {
t.equal(
err.message,
`400 Bad Request - GET ${OPTS.registry}ohno - badarg`,
'neatly printed message'
)
t.equal(err.code, 'E400', 'HTTP code used for err.code')
t.equal(err.statusCode, 400, 'numerical HTTP code available')
t.equal(err.method, 'GET', 'method in error object')
t.same(err.body, {
error: 'badarg',
}, 'parsed JSON error response available')
}
)
})
t.test('OTP error', t => {
tnock(t, OPTS.registry)
.get('/otplease')
.reply(401, { error: 'needs an otp, please' }, {
'www-authenticate': 'otp',
})
return fetch('/otplease', OPTS)
.then(
() => {
throw new Error('Should not have succeeded!')
},
err => {
t.equal(err.code, 'EOTP', 'got special OTP error code')
}
)
})
t.test('OTP error with prompt', t => {
let OTP = null
tnock(t, OPTS.registry)
.get('/otplease').times(2)
.matchHeader('npm-otp', otp => {
if (otp) {
OTP = otp[0]
t.strictSame(otp, ['12345'], 'got expected otp')
}
return true
})
.reply((...args) => {
if (OTP === '12345') {
return [200, { ok: 'this is fine' }, {}]
} else {
return [401, { error: 'otp, please' }, { 'www-authenticate': 'otp' }]
}
})
const otpPrompt = async () => '12345'
return fetch('/otplease', { ...OPTS, otpPrompt })
.then(res => {
t.strictSame(res.status, 200, 'got 200 response')
return res.json()
}).then(body => {
t.strictSame(body, { ok: 'this is fine' }, 'got expected body')
})
})
t.test('OTP error with prompt, expired OTP in settings', t => {
let OTP = null
tnock(t, OPTS.registry)
.get('/otplease').times(2)
.matchHeader('npm-otp', otp => {
if (otp) {
if (!OTP) {
t.strictSame(otp, ['98765'], 'got invalid otp first')
} else {
t.strictSame(otp, ['12345'], 'got expected otp')
}
OTP = otp[0]
}
return true
})
.reply((...args) => {
if (OTP === '12345') {
return [200, { ok: 'this is fine' }, {}]
} else {
return [401, { error: 'otp, please' }, { 'www-authenticate': 'otp' }]
}
})
const otpPrompt = async () => '12345'
return fetch('/otplease', { ...OPTS, otpPrompt, otp: '98765' })
.then(res => {
t.strictSame(res.status, 200, 'got 200 response')
return res.json()
}).then(body => {
t.strictSame(body, { ok: 'this is fine' }, 'got expected body')
})
})
t.test('OTP error with prompt that fails', t => {
tnock(t, OPTS.registry)
.get('/otplease')
.reply((...args) => {
return [401, { error: 'otp, please' }, { 'www-authenticate': 'otp' }]
})
const otpPrompt = async () => {
throw new Error('whoopsie')
}
return t.rejects(fetch('/otplease', { ...OPTS, otpPrompt }), errors.HttpErrorAuthOTP)
})
t.test('OTP error with prompt that returns nothing', t => {
tnock(t, OPTS.registry)
.get('/otplease')
.reply((...args) => {
return [401, { error: 'otp, please' }, { 'www-authenticate': 'otp' }]
})
const otpPrompt = async () => {}
return t.rejects(fetch('/otplease', { ...OPTS, otpPrompt }), errors.HttpErrorAuthOTP)
})
t.test('OTP error when missing www-authenticate', t => {
tnock(t, OPTS.registry)
.get('/otplease')
.reply(401, { error: 'needs a one-time password' })
return fetch('/otplease', OPTS)
.then(
() => {
throw new Error('Should not have succeeded!')
},
err => {
t.equal(
err.code,
'EOTP',
'got special OTP error code even with missing www-authenticate header'
)
}
)
})
t.test('Bad IP address error', t => {
tnock(t, OPTS.registry)
.get('/badaddr')
.reply(401, { error: 'you are using the wrong IP address, friend' }, {
'www-authenticate': 'ipaddress',
})
return fetch('/badaddr', OPTS)
.then(
() => {
throw new Error('Should not have succeeded!')
},
err => {
t.equal(err.code, 'EAUTHIP', 'got special OTP error code')
}
)
})
t.test('Unexpected www-authenticate error', t => {
tnock(t, OPTS.registry)
.get('/unown')
.reply(401, {
error: `
Pat-a-cake, pat-a-cake, baker's man.
Bake me a cake as fast as you can
Pat it, and prick it, and mark it with a "B"
And put it in the oven for baby and me!
`,
}, {
'www-authenticate': 'pattie-cake-protocol',
})
return fetch('/unown', OPTS)
.then(
() => {
throw new Error('Should not have succeeded!')
},
err => {
t.match(err.body.error, /Pat-a-cake/ig, 'error body explains it')
t.equal(err.code, 'E401', 'Unknown auth errors are generic 401s')
}
)
})
t.test('retries certain types')