Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit 2ba121e

Browse files
committed
Catch server.listen error and reject start promise
1 parent 0d3fa40 commit 2ba121e

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

lib/Server.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,25 @@ class Server {
127127
}
128128

129129
startServer(resolve, reject) {
130-
this._server.listen(this._options, error => {
131-
if (error) {
132-
tracer.error(`Error starting server: ${error.message}`);
133-
this._error = error;
134-
reject(error);
135-
} else {
136-
tracer.info(`Server started and listening at http://localhost:${this._options.port}`);
137-
this._error = null;
138-
resolve(this);
139-
}
140-
});
130+
try {
131+
this._server.listen(this._options, error => {
132+
if (error) {
133+
tracer.error(`Error starting server: ${error.message}`);
134+
this._serverStarting = false;
135+
this._serverStarted = false;
136+
this._error = error;
137+
reject(error);
138+
} else {
139+
tracer.info(`Server started and listening at http://localhost:${this._options.port}`);
140+
this._error = null;
141+
this._serverStarting = false;
142+
this._serverStarted = true;
143+
resolve(this);
144+
}
145+
});
146+
} catch (error) {
147+
reject(error);
148+
}
141149
}
142150

143151
get settings() {

test/unit/lib/Server.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ describe("Server", () => {
150150
});
151151

152152
describe("start method", () => {
153+
it("should reject the promise if an error occurs when calling to server listen method", async () => {
154+
const error = new Error("Foo error");
155+
libsMocks.stubs.http.createServer.listen.throws(error);
156+
157+
server = new Server(FOO_FEATURES_PATH);
158+
159+
try {
160+
await server.start();
161+
} catch (err) {
162+
expect(err).toEqual(error);
163+
}
164+
});
165+
153166
it("should call to server listen, and resolve the promise when started", async () => {
154167
libsMocks.stubs.http.createServer.onListen.returns(null);
155168

0 commit comments

Comments
 (0)