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

Commit

Permalink
fix: handle err on both start and stop echo-server (#569)
Browse files Browse the repository at this point in the history
* fix: handle err on both start and stop echo-server

* fix: add 'error' event to both server start and close

* chore: code review changes
  • Loading branch information
Pedro Santos authored and Alan Shaw committed Dec 11, 2019
1 parent d8b65ef commit d25c6f6
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/utils/echo-http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ module.exports.createServer = () => {

const server = http.createServer(handler)

server.start = (opts) => new Promise(
(resolve) => server.listen(Object.assign({ port: defaultPort, host: '127.0.0.1' }, opts), resolve)
)
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()
})
})

server.stop = () => new Promise((resolve) => server.close(resolve))
server.stop = () => new Promise((resolve, reject) => {
server.once('error', reject)
server.close((err) => {
server.removeListener('error', reject)
err ? reject(err) : resolve()
})
})

return server
}
Expand Down

0 comments on commit d25c6f6

Please sign in to comment.