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
Show file tree
Hide file tree
Changes from 3 commits
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
27 changes: 14 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('node:url')

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,17 @@ class HttpErrorBase extends Error {
constructor (method, res, body, spec) {
super()
this.name = this.constructor.name
this.headers = res.headers.raw()
console.log(res.headers)
lukekarrys marked this conversation as resolved.
Show resolved Hide resolved
lukekarrys marked this conversation as resolved.
Show resolved Hide resolved
this.headers = typeof res.headers?.raw === 'function' ? res.headers.raw() : res.headers
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 +46,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,
}
6 changes: 5 additions & 1 deletion test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,8 @@ t.test('Unexpected www-authenticate error', t => {
)
})

t.test('retries certain types')
t.test('error can take headers object', (t) => {
t.strictSame(new errors.HttpErrorBase('GET', { headers: { a: 1 } }).headers, { a: 1 })
t.strictSame(new errors.HttpErrorBase('GET', { }).headers, undefined)
t.end()
})