Skip to content

Commit

Permalink
feat(server): stop at startup failure (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
basmasking authored Jan 31, 2024
1 parent 4e3c244 commit c7d4463
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions packages/server-nodejs/src/JitarServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -96,9 +109,20 @@ export default class JitarServer

async stop(): Promise<void>
{
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');
}

Expand Down Expand Up @@ -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<void>
Expand Down

0 comments on commit c7d4463

Please sign in to comment.