Skip to content

Commit

Permalink
fix(intercept): Improve error message when new ClientMessage() is i…
Browse files Browse the repository at this point in the history
…nvoked 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.
  • Loading branch information
paulmelnikow authored and gr2m committed Jan 19, 2019
1 parent 8455e90 commit 6d2a312
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/intercept.js
Expand Up @@ -256,6 +256,16 @@ function overrideClientRequest() {

// Define the overriding client request that nock uses internally.
function OverriddenClientRequest(options, cb) {
if (!options) {
// In principle, there is no reason we couldn't support this. However it
// doesn't work, and fixing it seems low priority. Giving an explicit
// error seems nicer than crashing with a weird stack trace.
// https://github.com/nock/nock/pull/1386
throw Error(
'Creating a client request with empty `options` is not supported in Nock'
)
}

// TODO-coverage: Remove this conditional, as it is always true in Node 8+.
if (http.OutgoingMessage) http.OutgoingMessage.call(this)

Expand Down
2 changes: 1 addition & 1 deletion lib/request_overrider.js
Expand Up @@ -86,7 +86,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {

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

response.req = req

Expand Down

0 comments on commit 6d2a312

Please sign in to comment.