From 22790dbab6f955ce488e24f641b786968abc97f8 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 14 Mar 2022 00:49:43 +0200 Subject: [PATCH] feat: better handling of errors & app exiting --- packages/concordia-pinner/src/index.js | 27 ++++++++++++++----- .../concordia-pinner/src/utils/ipfsUtils.js | 5 ++-- .../src/logging/node/winstonLogUtils.js | 3 +++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/concordia-pinner/src/index.js b/packages/concordia-pinner/src/index.js index 430e7b8..0d7be43 100644 --- a/packages/concordia-pinner/src/index.js +++ b/packages/concordia-pinner/src/index.js @@ -12,20 +12,33 @@ import { logger } from './utils/logger'; let ipfsSingleton; +const exitApp = async (code = 0) => { + try { + if (ipfsSingleton) { + logger.info('Waiting IPFS singleton to stop...'); + await ipfsSingleton.stop(); + logger.info('IPFS singleton stopped.'); + } + } catch (e) { + console.log(e.message); + } + process.exit(code); +}; + +process.on('SIGTERM', exitApp); +process.on('SIGINT', exitApp); +process.on('SIGHUP', exitApp); + process.on('unhandledRejection', async (error) => { // This happens when attempting to initialize without any available Swarm addresses (e.g. Rendezvous) if (error.code === 'ERR_NO_VALID_ADDRESSES') { logger.error(`unhandledRejection: ${error.message}`); - process.exit(1); - } - - if (ipfsSingleton) { - await ipfsSingleton.stop(); + await exitApp(1); } - // Don't swallow other errors logger.error(error); - throw error; + const errorCode = error.code ? error.code : 0; + exitApp(errorCode); }); const getDeployedContract = async (web3) => { diff --git a/packages/concordia-pinner/src/utils/ipfsUtils.js b/packages/concordia-pinner/src/utils/ipfsUtils.js index aab7f58..f7386a6 100644 --- a/packages/concordia-pinner/src/utils/ipfsUtils.js +++ b/packages/concordia-pinner/src/utils/ipfsUtils.js @@ -23,7 +23,7 @@ const getRendezvousMultiaddress = () => { const multiaddress = `${host}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`; if (isMultiaddr(multiaddress)) return new Multiaddr(multiaddress); - process.exit(1); // Invalid multiaddress - exiting with error + throw Error('Invalid multiaddress'); }; export const getResolvedRendezvousMultiaddress = async () => { @@ -36,8 +36,7 @@ export const getResolvedRendezvousMultiaddress = async () => { const resolvedDomain = await dnsLookup(address); return Promise.resolve(new Multiaddr(`/ip4/${resolvedDomain.address}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`)); } catch (error) { - logger.error(new Error(`DNS lookup of ${address} failed.\n${error}`)); - process.exit(2); + throw new Error(`DNS lookup of ${address} failed.\n${error}`); } } return Promise.resolve(rendezvousMultiaddress); diff --git a/packages/concordia-shared/src/logging/node/winstonLogUtils.js b/packages/concordia-shared/src/logging/node/winstonLogUtils.js index e637748..16567a8 100644 --- a/packages/concordia-shared/src/logging/node/winstonLogUtils.js +++ b/packages/concordia-shared/src/logging/node/winstonLogUtils.js @@ -18,6 +18,7 @@ const getLogger = (winston, logsDirectory, serviceName) => { if (process.env.NODE_ENV !== 'production') { transports.push(new winston.transports.Console({ format: winston.format.combine( + winston.format.errors({ stack: true }), winston.format.colorize(), winston.format.timestamp(), winston.format.simple(), @@ -26,6 +27,7 @@ const getLogger = (winston, logsDirectory, serviceName) => { exceptionHandlers.push(new winston.transports.Console({ format: winston.format.combine( + winston.format.errors({ stack: true }), winston.format.colorize(), winston.format.timestamp(), winston.format.simple(), @@ -36,6 +38,7 @@ const getLogger = (winston, logsDirectory, serviceName) => { return winston.createLogger({ level: process.env.NODE_ENV === 'production' ? 'info' : 'silly', format: winston.format.combine( + winston.format.errors({ stack: true }), winston.format.timestamp(), winston.format.json(), ),