From d25c6f6631749635591dd38ea3f76fbaf4285132 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Wed, 11 Dec 2019 12:34:02 +0000 Subject: [PATCH] fix: handle err on both start and stop echo-server (#569) * fix: handle err on both start and stop echo-server * fix: add 'error' event to both server start and close * chore: code review changes --- src/utils/echo-http-server.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/utils/echo-http-server.js b/src/utils/echo-http-server.js index 8e7f059e..3cdeabd7 100644 --- a/src/utils/echo-http-server.js +++ b/src/utils/echo-http-server.js @@ -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 }