From 2e69aea7246e82220b03b968923cfe637a8bc7e8 Mon Sep 17 00:00:00 2001 From: Bas Meeuwissen Date: Wed, 31 Jan 2024 10:58:36 +0100 Subject: [PATCH] #449: stop the server in case of failure on startup --- packages/server-nodejs/src/JitarServer.ts | 39 ++++++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/server-nodejs/src/JitarServer.ts b/packages/server-nodejs/src/JitarServer.ts index f8da8243..ac339bff 100644 --- a/packages/server-nodejs/src/JitarServer.ts +++ b/packages/server-nodejs/src/JitarServer.ts @@ -86,8 +86,21 @@ export default class JitarServer { const url = new URL(this.#configuration.url ?? RuntimeDefaults.URL); - await this.#startApplication(); - await this.#startServer(url.port); + try + { + await this.#startApplication(); + await this.#startServer(url.port); + } + catch (error: unknown) + { + const message = error instanceof Error ? error.message : String(error); + + this.#logger.error(`Failed to start server -> ${message}`); + + await this.stop(); + + return; + } this.#printProcedureInfo(); @@ -96,9 +109,20 @@ export default class JitarServer async stop(): Promise { - await this.#stopServer(); - await this.#stopApplication(); + try + { + await this.#stopServer(); + await this.#stopApplication(); + } + catch (error: unknown) + { + const message = error instanceof Error ? error.message : String(error); + this.#logger.error(`Caught error while stopping server -> ${message}`); + + return; + } + this.#logger.info('Server stopped'); } @@ -226,7 +250,12 @@ export default class JitarServer return Promise.resolve(); } - return new Promise(resolve => { this.#server = this.#app.listen(port, resolve); }); + return new Promise((resolve, reject) => + { + this.#server = this.#app.listen(port, resolve); + + this.#server.on('error', reject); + }); } #stopServer(): Promise