From 1e08f09117787458a0b791d035e3a5098ef9c2f5 Mon Sep 17 00:00:00 2001 From: Ryan Wheale Date: Thu, 5 Jan 2023 11:39:16 -0700 Subject: [PATCH] style: move validation/throw code per code review --- lib/scope.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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}`