1'use strict' 2 3const url = require('url') 4 5function packageName (href) { 6 try { 7 let basePath = url.parse(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 } 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.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} 35module.exports.HttpErrorBase = HttpErrorBase 36 37class 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} 50module.exports.HttpErrorGeneral = HttpErrorGeneral 51 52class 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} 60module.exports.HttpErrorAuthOTP = HttpErrorAuthOTP 61 62class 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} 70module.exports.HttpErrorAuthIPAddress = HttpErrorAuthIPAddress 71 72class 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} 79module.exports.HttpErrorAuthUnknown = HttpErrorAuthUnknown 80