Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make ErrorBase always capture stack trace #239

Merged
merged 4 commits into from
May 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const url = require('url')
const { URL } = require('url')
lukekarrys marked this conversation as resolved.
Show resolved Hide resolved

function packageName (href) {
try {
let basePath = new url.URL(href).pathname.slice(1)
let basePath = new URL(href).pathname.slice(1)
if (!basePath.match(/^-/)) {
basePath = basePath.split('/')
var index = basePath.indexOf('_rewrite')
Expand All @@ -15,7 +15,7 @@ function packageName (href) {
}
return decodeURIComponent(basePath[index])
}
} catch (_) {
} catch {
// this is ok
}
}
Expand All @@ -24,16 +24,16 @@ class HttpErrorBase extends Error {
constructor (method, res, body, spec) {
super()
this.name = this.constructor.name
this.headers = res.headers.raw()
this.headers = typeof res.headers.raw === 'function' ? res.headers.raw() : res.headers
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
this.statusCode = res.status
this.code = `E${res.status}`
this.method = method
this.uri = res.url
this.body = body
this.pkgid = spec ? spec.toString() : packageName(res.url)
Error.captureStackTrace(this, this.constructor)
}
}
module.exports.HttpErrorBase = HttpErrorBase

class HttpErrorGeneral extends HttpErrorBase {
constructor (method, res, body, spec) {
Expand All @@ -45,36 +45,36 @@ class HttpErrorGeneral extends HttpErrorBase {
}${
(body && body.error) ? ' - ' + body.error : ''
}`
Error.captureStackTrace(this, HttpErrorGeneral)
}
}
module.exports.HttpErrorGeneral = HttpErrorGeneral

class HttpErrorAuthOTP extends HttpErrorBase {
constructor (method, res, body, spec) {
super(method, res, body, spec)
this.message = 'OTP required for authentication'
this.code = 'EOTP'
Error.captureStackTrace(this, HttpErrorAuthOTP)
}
}
module.exports.HttpErrorAuthOTP = HttpErrorAuthOTP

class HttpErrorAuthIPAddress extends HttpErrorBase {
constructor (method, res, body, spec) {
super(method, res, body, spec)
this.message = 'Login is not allowed from your IP address'
this.code = 'EAUTHIP'
Error.captureStackTrace(this, HttpErrorAuthIPAddress)
}
}
module.exports.HttpErrorAuthIPAddress = HttpErrorAuthIPAddress

class HttpErrorAuthUnknown extends HttpErrorBase {
constructor (method, res, body, spec) {
super(method, res, body, spec)
this.message = 'Unable to authenticate, need: ' + res.headers.get('www-authenticate')
Error.captureStackTrace(this, HttpErrorAuthUnknown)
}
}
module.exports.HttpErrorAuthUnknown = HttpErrorAuthUnknown

module.exports = {
HttpErrorBase,
HttpErrorGeneral,
HttpErrorAuthOTP,
HttpErrorAuthIPAddress,
HttpErrorAuthUnknown,
}