diff --git a/lib/scope.js b/lib/scope.js index cf0b246f6..82f3bcc42 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -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 { @@ -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}`