Skip to content

Commit 45cef0a

Browse files
lukekarryswraithgar
andauthoredMay 2, 2024
fix: make ErrorBase always capture stack trace (#239)
fix: allow HttpErrorBase to take headers object Co-authored-by: Gar <[email protected]>
1 parent 3fc23e9 commit 45cef0a

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed
 

‎lib/errors.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

3-
const url = require('url')
3+
const { URL } = require('node:url')
44

55
function packageName (href) {
66
try {
7-
let basePath = new url.URL(href).pathname.slice(1)
7+
let basePath = new URL(href).pathname.slice(1)
88
if (!basePath.match(/^-/)) {
99
basePath = basePath.split('/')
1010
var index = basePath.indexOf('_rewrite')
@@ -15,7 +15,7 @@ function packageName (href) {
1515
}
1616
return decodeURIComponent(basePath[index])
1717
}
18-
} catch (_) {
18+
} catch {
1919
// this is ok
2020
}
2121
}
@@ -24,16 +24,16 @@ class HttpErrorBase extends Error {
2424
constructor (method, res, body, spec) {
2525
super()
2626
this.name = this.constructor.name
27-
this.headers = res.headers.raw()
27+
this.headers = typeof res.headers?.raw === 'function' ? res.headers.raw() : res.headers
2828
this.statusCode = res.status
2929
this.code = `E${res.status}`
3030
this.method = method
3131
this.uri = res.url
3232
this.body = body
3333
this.pkgid = spec ? spec.toString() : packageName(res.url)
34+
Error.captureStackTrace(this, this.constructor)
3435
}
3536
}
36-
module.exports.HttpErrorBase = HttpErrorBase
3737

3838
class HttpErrorGeneral extends HttpErrorBase {
3939
constructor (method, res, body, spec) {
@@ -45,36 +45,36 @@ class HttpErrorGeneral extends HttpErrorBase {
4545
}${
4646
(body && body.error) ? ' - ' + body.error : ''
4747
}`
48-
Error.captureStackTrace(this, HttpErrorGeneral)
4948
}
5049
}
51-
module.exports.HttpErrorGeneral = HttpErrorGeneral
5250

5351
class HttpErrorAuthOTP extends HttpErrorBase {
5452
constructor (method, res, body, spec) {
5553
super(method, res, body, spec)
5654
this.message = 'OTP required for authentication'
5755
this.code = 'EOTP'
58-
Error.captureStackTrace(this, HttpErrorAuthOTP)
5956
}
6057
}
61-
module.exports.HttpErrorAuthOTP = HttpErrorAuthOTP
6258

6359
class HttpErrorAuthIPAddress extends HttpErrorBase {
6460
constructor (method, res, body, spec) {
6561
super(method, res, body, spec)
6662
this.message = 'Login is not allowed from your IP address'
6763
this.code = 'EAUTHIP'
68-
Error.captureStackTrace(this, HttpErrorAuthIPAddress)
6964
}
7065
}
71-
module.exports.HttpErrorAuthIPAddress = HttpErrorAuthIPAddress
7266

7367
class HttpErrorAuthUnknown extends HttpErrorBase {
7468
constructor (method, res, body, spec) {
7569
super(method, res, body, spec)
7670
this.message = 'Unable to authenticate, need: ' + res.headers.get('www-authenticate')
77-
Error.captureStackTrace(this, HttpErrorAuthUnknown)
7871
}
7972
}
80-
module.exports.HttpErrorAuthUnknown = HttpErrorAuthUnknown
73+
74+
module.exports = {
75+
HttpErrorBase,
76+
HttpErrorGeneral,
77+
HttpErrorAuthOTP,
78+
HttpErrorAuthIPAddress,
79+
HttpErrorAuthUnknown,
80+
}

‎test/errors.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,8 @@ t.test('Unexpected www-authenticate error', t => {
271271
)
272272
})
273273

274-
t.test('retries certain types')
274+
t.test('error can take headers object', (t) => {
275+
t.strictSame(new errors.HttpErrorBase('GET', { headers: { a: 1 } }).headers, { a: 1 })
276+
t.strictSame(new errors.HttpErrorBase('GET', { }).headers, undefined)
277+
t.end()
278+
})

0 commit comments

Comments
 (0)