Browse Source

feat: better handling of errors & app exiting

develop
Ezerous 3 years ago
parent
commit
22790dbab6
  1. 27
      packages/concordia-pinner/src/index.js
  2. 5
      packages/concordia-pinner/src/utils/ipfsUtils.js
  3. 3
      packages/concordia-shared/src/logging/node/winstonLogUtils.js

27
packages/concordia-pinner/src/index.js

@ -12,20 +12,33 @@ import { logger } from './utils/logger';
let ipfsSingleton; 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) => { process.on('unhandledRejection', async (error) => {
// This happens when attempting to initialize without any available Swarm addresses (e.g. Rendezvous) // This happens when attempting to initialize without any available Swarm addresses (e.g. Rendezvous)
if (error.code === 'ERR_NO_VALID_ADDRESSES') { if (error.code === 'ERR_NO_VALID_ADDRESSES') {
logger.error(`unhandledRejection: ${error.message}`); logger.error(`unhandledRejection: ${error.message}`);
process.exit(1); await exitApp(1);
}
if (ipfsSingleton) {
await ipfsSingleton.stop();
} }
// Don't swallow other errors
logger.error(error); logger.error(error);
throw error; const errorCode = error.code ? error.code : 0;
exitApp(errorCode);
}); });
const getDeployedContract = async (web3) => { const getDeployedContract = async (web3) => {

5
packages/concordia-pinner/src/utils/ipfsUtils.js

@ -23,7 +23,7 @@ const getRendezvousMultiaddress = () => {
const multiaddress = `${host}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`; const multiaddress = `${host}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`;
if (isMultiaddr(multiaddress)) return new Multiaddr(multiaddress); if (isMultiaddr(multiaddress)) return new Multiaddr(multiaddress);
process.exit(1); // Invalid multiaddress - exiting with error throw Error('Invalid multiaddress');
}; };
export const getResolvedRendezvousMultiaddress = async () => { export const getResolvedRendezvousMultiaddress = async () => {
@ -36,8 +36,7 @@ export const getResolvedRendezvousMultiaddress = async () => {
const resolvedDomain = await dnsLookup(address); const resolvedDomain = await dnsLookup(address);
return Promise.resolve(new Multiaddr(`/ip4/${resolvedDomain.address}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`)); return Promise.resolve(new Multiaddr(`/ip4/${resolvedDomain.address}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`));
} catch (error) { } catch (error) {
logger.error(new Error(`DNS lookup of ${address} failed.\n${error}`)); throw new Error(`DNS lookup of ${address} failed.\n${error}`);
process.exit(2);
} }
} }
return Promise.resolve(rendezvousMultiaddress); return Promise.resolve(rendezvousMultiaddress);

3
packages/concordia-shared/src/logging/node/winstonLogUtils.js

@ -18,6 +18,7 @@ const getLogger = (winston, logsDirectory, serviceName) => {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
transports.push(new winston.transports.Console({ transports.push(new winston.transports.Console({
format: winston.format.combine( format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.colorize(), winston.format.colorize(),
winston.format.timestamp(), winston.format.timestamp(),
winston.format.simple(), winston.format.simple(),
@ -26,6 +27,7 @@ const getLogger = (winston, logsDirectory, serviceName) => {
exceptionHandlers.push(new winston.transports.Console({ exceptionHandlers.push(new winston.transports.Console({
format: winston.format.combine( format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.colorize(), winston.format.colorize(),
winston.format.timestamp(), winston.format.timestamp(),
winston.format.simple(), winston.format.simple(),
@ -36,6 +38,7 @@ const getLogger = (winston, logsDirectory, serviceName) => {
return winston.createLogger({ return winston.createLogger({
level: process.env.NODE_ENV === 'production' ? 'info' : 'silly', level: process.env.NODE_ENV === 'production' ? 'info' : 'silly',
format: winston.format.combine( format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.timestamp(), winston.format.timestamp(),
winston.format.json(), winston.format.json(),
), ),

Loading…
Cancel
Save