Skip to content

Commit

Permalink
style: move validation/throw code per code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Wheale committed Jan 5, 2023
1 parent b842a4e commit 1e08f09
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/scope.js
Expand Up @@ -32,7 +32,13 @@ function normalizeUrl(u) {
if (u instanceof LegacyUrl) {
return normalizeUrl(new URL(url.format(u)))
}
throw new Error('Unrecognized URL')
throw new Error('Unrecognized URL passed to nock()')
}

if (!/https?:/.test(u.protocol)) {
throw new TypeError(
`Protocol '${u.protocol}' not recognized. This commonly occurs when a hostname and port are included without a protocol, producing a URL that is valid but confusing, and probably not what you want.`
)
}

return {
Expand Down Expand Up @@ -89,11 +95,6 @@ class Scope extends EventEmitter {

if (!(basePath instanceof RegExp)) {
this.urlParts = normalizeUrl(basePath)
if (!/https?:/.test(this.urlParts.protocol)) {
throw new TypeError(
`Protocol '${this.urlParts.protocol}' not recognized. This commonly occurs when a hostname and port are included without a protocol, producing a URL that is valid but confusing, and probably not what you want.`
)
}
this.port = this.urlParts.port
this.basePathname = this.urlParts.pathname.replace(/\/$/, '')
this.basePath = `${this.urlParts.protocol}//${this.urlParts.hostname}:${this.port}`
Expand Down

0 comments on commit 1e08f09

Please sign in to comment.