Skip to content

Commit

Permalink
feat(index.ts): wrap main function in try-catch block to handle poten…
Browse files Browse the repository at this point in the history
…tial errors

fix(index.ts): change process.exit code from 1 to 0 on normal termination to follow standard conventions
feat(index.ts): add error logging in catch block to improve error visibility and debugging
  • Loading branch information
MartinMinkov committed Aug 30, 2023
1 parent e9ccb94 commit 42c094a
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import { buildServer } from './server';
const PORT = process.env.PORT || 8080;

(async function main() {
const context = await buildContext(process.env.PG_CONN);
const server = await buildServer(context);
try {
const context = await buildContext(process.env.PG_CONN);
const server = await buildServer(context);

['SIGINT', 'SIGTERM', 'SIGQUIT'].forEach((signal) => {
process.on(signal, () => server.close());
});
server.on('close', async () => {
await context.db_client.close();
process.exit(1);
});
server.listen(PORT, () => {
console.info(`Server is running on port: ${PORT}`);
});

server.listen(PORT, () => {
console.info(`Server is running on port: ${PORT}`);
});
['SIGINT', 'SIGTERM', 'SIGQUIT'].forEach((signal) => {
process.on(signal, () => server.close());
});

server.on('close', async () => {
await context.db_client.close();
process.exit(0); // normal termination
});
} catch (error) {
console.error('An error occurred:', error);
process.exit(1); // exit with an error code
}
})();

0 comments on commit 42c094a

Please sign in to comment.