1
1
'use strict'
2
2
3
- const url = require ( 'url' )
3
+ const { URL } = require ( 'node: url' )
4
4
5
5
function packageName ( href ) {
6
6
try {
7
- let basePath = new url . URL ( href ) . pathname . slice ( 1 )
7
+ let basePath = new URL ( href ) . pathname . slice ( 1 )
8
8
if ( ! basePath . match ( / ^ - / ) ) {
9
9
basePath = basePath . split ( '/' )
10
10
var index = basePath . indexOf ( '_rewrite' )
@@ -15,7 +15,7 @@ function packageName (href) {
15
15
}
16
16
return decodeURIComponent ( basePath [ index ] )
17
17
}
18
- } catch ( _ ) {
18
+ } catch {
19
19
// this is ok
20
20
}
21
21
}
@@ -24,16 +24,16 @@ class HttpErrorBase extends Error {
24
24
constructor ( method , res , body , spec ) {
25
25
super ( )
26
26
this . name = this . constructor . name
27
- this . headers = res . headers . raw ( )
27
+ this . headers = typeof res . headers ? .raw === 'function' ? res . headers . raw ( ) : res . headers
28
28
this . statusCode = res . status
29
29
this . code = `E${ res . status } `
30
30
this . method = method
31
31
this . uri = res . url
32
32
this . body = body
33
33
this . pkgid = spec ? spec . toString ( ) : packageName ( res . url )
34
+ Error . captureStackTrace ( this , this . constructor )
34
35
}
35
36
}
36
- module . exports . HttpErrorBase = HttpErrorBase
37
37
38
38
class HttpErrorGeneral extends HttpErrorBase {
39
39
constructor ( method , res , body , spec ) {
@@ -45,36 +45,36 @@ class HttpErrorGeneral extends HttpErrorBase {
45
45
} ${
46
46
( body && body . error ) ? ' - ' + body . error : ''
47
47
} `
48
- Error . captureStackTrace ( this , HttpErrorGeneral )
49
48
}
50
49
}
51
- module . exports . HttpErrorGeneral = HttpErrorGeneral
52
50
53
51
class HttpErrorAuthOTP extends HttpErrorBase {
54
52
constructor ( method , res , body , spec ) {
55
53
super ( method , res , body , spec )
56
54
this . message = 'OTP required for authentication'
57
55
this . code = 'EOTP'
58
- Error . captureStackTrace ( this , HttpErrorAuthOTP )
59
56
}
60
57
}
61
- module . exports . HttpErrorAuthOTP = HttpErrorAuthOTP
62
58
63
59
class HttpErrorAuthIPAddress extends HttpErrorBase {
64
60
constructor ( method , res , body , spec ) {
65
61
super ( method , res , body , spec )
66
62
this . message = 'Login is not allowed from your IP address'
67
63
this . code = 'EAUTHIP'
68
- Error . captureStackTrace ( this , HttpErrorAuthIPAddress )
69
64
}
70
65
}
71
- module . exports . HttpErrorAuthIPAddress = HttpErrorAuthIPAddress
72
66
73
67
class HttpErrorAuthUnknown extends HttpErrorBase {
74
68
constructor ( method , res , body , spec ) {
75
69
super ( method , res , body , spec )
76
70
this . message = 'Unable to authenticate, need: ' + res . headers . get ( 'www-authenticate' )
77
- Error . captureStackTrace ( this , HttpErrorAuthUnknown )
78
71
}
79
72
}
80
- module . exports . HttpErrorAuthUnknown = HttpErrorAuthUnknown
73
+
74
+ module . exports = {
75
+ HttpErrorBase,
76
+ HttpErrorGeneral,
77
+ HttpErrorAuthOTP,
78
+ HttpErrorAuthIPAddress,
79
+ HttpErrorAuthUnknown,
80
+ }
0 commit comments