Skip to content

POC Async Reply functions.#1572

Closed
mastermatt wants to merge 2 commits intonock:betafrom
mastermatt:async-reply-fns
Closed

POC Async Reply functions.#1572
mastermatt wants to merge 2 commits intonock:betafrom
mastermatt:async-reply-fns

Conversation

@mastermatt
Copy link
Copy Markdown
Member

This is a POC for #1557.

Besides adding the extra functionality, I think it cleans up the massive function .end() a little. It removes the need for continueWithResponseBody to have an error first signature.

The area where I question this logic is around error handling, but this has roots in the current code. My question is, what should happen to errors from inside user provided reply functions?

Current logic:

Throw an error without accepting a callback arg

nock('http://example.com').get('/').reply((path, requestBody) => {
  throw Error('boom!')
})

http.get('http://example.com', res => {
  console.log(res)
})
Error: boom!
    at Interceptor.nock.get.reply [as fullReplyFunction] (./nock/test.js:7:9)
    at end (./nock/lib/request_overrider.js:339:43)
    at ./nock/lib/request_overrider.js:141:9
    at OverriddenClientRequest.RequestOverrider.req.write (./nock/lib/request_overrider.js:115:9)
    at OverriddenClientRequest.RequestOverrider.req.end (./nock/lib/request_overrider.js:137:11)
    at Object.module.get (./nock/lib/common.js:114:11)

Throw an error with accepting a callback arg

nock('http://example.com').get('/').reply((path, requestBody, callback) => {
  throw Error('boom!')
})

http.get('http://example.com', res => {
  console.log(res)
})
Error: boom!
    <same stack trace as above>

Passing an error to the callback

nock('http://example.com').get('/').reply((path, requestBody, callback) => {
  callback(Error('boom!'))
})

http.get('http://example.com', res => {
  console.log(res)
})
IncomingMessage<statusCode: 500>
data event -> buffer -> toString: 
Error: boom!
    <same stack trace as above>

With proposed logic

  • Throw an error without accepting a callback arg: stays the same
  • Throw an error with accepting a callback arg: Returns a 500 instead of bubbling up.
  • Passing an error to the callback: stays the same

New use case: throw an error inside an async function (or return a rejecting promise)

nock('http://example.com').get('/').reply(async (path, requestBody) => {
  throw Error('boom!')
})

http.get('http://example.com', res => {
  console.log(res)
})
IncomingMessage<statusCode: 500>
data event -> buffer -> toString: 
Error: boom!
    <same stack trace as above>

So it's kinda the same 🤷‍♂. I wish all these scenarios did one or the other.
In general, I question why we sometimes want errors to result in valid responses with 500 status codes, instead of letting the user defined function catch errors and return the 500 manually. But that might be out of scope for this topic and I'm overthinking it.

@paulmelnikow
Copy link
Copy Markdown
Member

This looks nice! Will respond to your questions hopefully tomorrow.

Some preliminary feedback is that I think it might be worth sorting out the uncovered lines adjacent to these functions, and either adding tests for them or getting really confident that they aren't reachable. Probably that can be done in a separate PR that goes in before this one. The most important reason to get coverage to or near 100% is that it reduces the chance of breaking things when we refactor. The other reason is that asserting 100% coverage means we won't inadvertently pull in new code paths that are untested, or leave unused code paths after refactoring.

@mastermatt
Copy link
Copy Markdown
Member Author

All the touched lines have 100% coverage, but Coveralls is complaining because the total number of lines in the lib was reduced a bit, increasing the uncovered percentage.

This check is no longer reachable because `util.promisfy` will ignore
subsequent calls to the callback method.
@mastermatt
Copy link
Copy Markdown
Member Author

Went with Option B instead.

@mastermatt mastermatt closed this Aug 1, 2019
@mastermatt mastermatt deleted the async-reply-fns branch August 1, 2019 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants