Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serer): handle exceptions during start and stop #457

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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