Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

fix: handle err on both start and stop echo-server #569

Merged
merged 3 commits into from
Dec 11, 2019

Conversation

PedroMiguelSS
Copy link
Contributor

echo-server was not handling properly error on both start() and stop() methods.

@achingbrain suggested on #565 's review:

server.start = (opts) => new Promise((resolve, reject) => {
  server.once('error', reject)
  server.listen(Object.assign({ port: defaultPort, host: '127.0.0.1' }, opts), () => {
    server.removeListener('error', reject)
    resolve()
  })
})

I think we just want to check server.listen() and server.close() related errors. For this reason, my suggestion is:

server.start = (opts) => new Promise(
  (resolve, reject) => server.listen(
    Object.assign({ port: defaultPort, host: '127.0.0.1' }, opts), (err) => err ? reject(err) : resolve()
  )
)

@achingbrain
Copy link
Collaborator

You need to register for the error event as well. The callback passed to .listen() is only added as a listener for the listening event.

https://nodejs.org/api/net.html#net_server_listen

@hugomrdias
Copy link
Contributor

promisifing events is tricky business, do you have any example of a use-case where the error coming from .listen() isnt enough ?

@achingbrain
Copy link
Collaborator

achingbrain commented Dec 9, 2019

My point is there will be no error passed to the callback passed to .listen().

When you start a server you'll either get a listening event (happy path) or an error event (unhappy path) - the callback passed to .listen() is only registered for the listening event.

@hugomrdias
Copy link
Contributor

hugomrdias commented Dec 9, 2019

humm right i didn't got that the callback is just a listener to the listening event.

This makes more sense now :) plus as we are only wrapping it in a new Promise should be safe.

server.start = (opts) => new Promise((resolve, reject) => {
  server.once('error', reject)
  server.listen(Object.assign({ port: defaultPort, host: '127.0.0.1' }, opts), () => {
    server.removeListener('error', reject)
    resolve()
  })
})

@PedroMiguelSS
Copy link
Contributor Author

@achingbrain , can you give me a second review here, please?

server.stop = () => new Promise((resolve) => server.close(resolve))
server.stop = () => new Promise((resolve, reject) => {
server.once('error', reject)
server.close(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the callback can be called with an Error if the server was not open

https://github.com/nodejs/node/blob/9cfb3841785cd658bb13369ed642247e089a5533/lib/net.js#L1519

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that on docs but I thought server.once('error', reject) would call its callback on any error (including the [ERR_SERVER_NOT_RUNNING]).

Copy link
Contributor Author

@PedroMiguelSS PedroMiguelSS Dec 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, do we really need to listen error event in this case?

@alanshaw alanshaw merged commit d25c6f6 into master Dec 11, 2019
@alanshaw alanshaw deleted the fix/promisify-echo-server branch December 11, 2019 12:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants