From 76546a93212663c8a9c99bf4a9e63dc4118e0554 Mon Sep 17 00:00:00 2001 From: Saihajpreet Singh Date: Mon, 8 Jan 2024 14:12:48 -0600 Subject: [PATCH] feat: imrpove exception handling --- packages/indexer-agent/src/index.ts | 22 ++++++++++++++++++++++ packages/indexer-service/src/index.ts | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/packages/indexer-agent/src/index.ts b/packages/indexer-agent/src/index.ts index ebe961689..3026f21e2 100644 --- a/packages/indexer-agent/src/index.ts +++ b/packages/indexer-agent/src/index.ts @@ -11,6 +11,7 @@ import { startMultiNetwork, parseNetworkSpecifications, } from './commands/start-multi-network' +import process from 'node:process' const MULTINETWORK_MODE: boolean = !!process.env.INDEXER_AGENT_MULTINETWORK_MODE && @@ -70,4 +71,25 @@ async function main(): Promise { await processArgumentsAndRun(args) } +const exceptionLogger = createLogger({ + name: 'IndexerAgent', + async: false, +}) + +process.on('uncaughtException', (reason, promise) => { + exceptionLogger.error('Uncaught exception', { + reason, + promise, + }) + process.exit(1) +}) + +process.on('unhandledRejection', (reason, promise) => { + exceptionLogger.error('Unhandled rejection', { + reason, + promise, + }) + process.exit(1) +}) + void main() diff --git a/packages/indexer-service/src/index.ts b/packages/indexer-service/src/index.ts index 7add4940b..d3de07857 100644 --- a/packages/indexer-service/src/index.ts +++ b/packages/indexer-service/src/index.ts @@ -1,6 +1,8 @@ +import process from 'process' import * as yargs from 'yargs' import start from './commands/start' +import { createLogger } from '@graphprotocol/common-ts' yargs .scriptName('indexer-service') @@ -8,3 +10,24 @@ yargs .command(start) .demandCommand(1, 'Choose a command from the above list') .help().argv + +const exceptionLogger = createLogger({ + name: 'IndexerService', + async: false, +}) + +process.on('uncaughtException', (reason, promise) => { + exceptionLogger.error('Uncaught exception', { + reason, + promise, + }) + process.exit(1) +}) + +process.on('unhandledRejection', (reason, promise) => { + exceptionLogger.error('Unhandled rejection', { + reason, + promise, + }) + process.exit(1) +})