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