Skip to content

Commit 6d2a312

Browse files
paulmelnikowgr2m
authored andcommitted
fix(intercept): Improve error message when new ClientMessage() is invoked with no options (#1386)
In Node, it’s valid to invoke `new ClientMessage()` with no options. However, in Nock, this causes a crash: Cannot read property 'proto' of undefined stack: | Object.normalizeRequestOptions (lib/common.js:12:27) interceptorsFor (lib/intercept.js:139:10) new OverriddenClientRequest (lib/intercept.js:263:26) Test.t (tests/test_request_overrider.js:20:8) Fixing that does not seem like a high priority, so this provides a better error message, while also removing related unreachable code.
1 parent 8455e90 commit 6d2a312

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/intercept.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ function overrideClientRequest() {
256256

257257
// Define the overriding client request that nock uses internally.
258258
function OverriddenClientRequest(options, cb) {
259+
if (!options) {
260+
// In principle, there is no reason we couldn't support this. However it
261+
// doesn't work, and fixing it seems low priority. Giving an explicit
262+
// error seems nicer than crashing with a weird stack trace.
263+
// https://github.com/nock/nock/pull/1386
264+
throw Error(
265+
'Creating a client request with empty `options` is not supported in Nock'
266+
)
267+
}
268+
259269
// TODO-coverage: Remove this conditional, as it is always true in Node 8+.
260270
if (http.OutgoingMessage) http.OutgoingMessage.call(this)
261271

lib/request_overrider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
8686

8787
// We may be changing the options object and we don't want those
8888
// changes affecting the user so we use a clone of the object.
89-
options = _.clone(options) || {}
89+
options = _.clone(options)
9090

9191
response.req = req
9292

0 commit comments

Comments
 (0)