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