Skip to content

Commit 9bd9499

Browse files
committed
fix: close json on error
1 parent ddf8487 commit 9bd9499

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { program } from './command/index.js';
77
import { logError, logInfo } from './utils/logging/console.js';
88
import { withInterruptHandling } from './utils/interrupt-handler.js';
99
import './programs/index.js';
10-
import { disconnectWalletConnect } from './utils/index.js';
10+
import {
11+
closeJsonLogging,
12+
disconnectWalletConnect,
13+
logJson,
14+
openJsonLogging,
15+
} from './utils/index.js';
1116

1217
export * from './utils/index.js';
1318

@@ -60,7 +65,7 @@ const runCLI = withInterruptHandling(async () => {
6065
const isJson = process.argv.includes('--json');
6166
const chain = await getChain();
6267
if (isJson) {
63-
console.info('[');
68+
openJsonLogging();
6469
} else {
6570
logInfo(`${'-'.repeat(100)}`);
6671
logInfo(`Using chain: Name: ${chain.name}, Chain ID: ${chain.id}`);
@@ -71,13 +76,18 @@ const runCLI = withInterruptHandling(async () => {
7176

7277
runCLI()
7378
.catch(async (error) => {
74-
logError('CLI Error:', error.message);
75-
await disconnectWalletConnect();
79+
await disconnectWalletConnect().catch();
80+
if (program.opts().json) {
81+
logJson({ error: error.message });
82+
closeJsonLogging();
83+
} else {
84+
logError('CLI Error:', error.message);
85+
}
7686
process.exit(1);
7787
})
7888
.finally(async () => {
7989
if (program.opts().json) {
80-
console.info(']');
90+
closeJsonLogging();
8191
} else {
8292
const chain = await getChain();
8393
if (chain.id === mainnet.id) {

utils/interrupt-handler.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import process from 'process';
22

3-
import { logError, logInfo } from './logging/console.js';
3+
import {
4+
closeJsonLogging,
5+
logError,
6+
logInfo,
7+
logJson,
8+
} from './logging/console.js';
49
import { disconnectWalletConnect } from './wallet-connect.js';
510

611
type ActionHandler = (...args: any[]) => Promise<any>;
@@ -27,10 +32,17 @@ export const withInterruptHandling = (action: ActionHandler) => {
2732

2833
return result;
2934
} catch (err) {
30-
if (err instanceof Error) logError('Command failed:', err.message);
31-
else logError('Command failed:', err);
35+
await disconnectWalletConnect().catch(() => {});
36+
// programm.opts is not init
37+
const isJson = process.argv.includes('--json');
38+
if (isJson) {
39+
logJson({ error: err instanceof Error ? err.message : err });
40+
closeJsonLogging();
41+
} else {
42+
if (err instanceof Error) logError('Command failed:', err.message);
43+
else logError('Command failed:', err);
44+
}
3245

33-
await disconnectWalletConnect();
3446
process.exit(1);
3547
} finally {
3648
process.off('SIGINT', sigintHandler);

utils/logging/console.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,24 @@ const bigIntStringify = <T>(value: T): string => {
2828
);
2929
};
3030

31+
// Flag so that next log can add comma if previous log is JSON to create valid JSON array output
3132
let IS_PREV_JSON_LOG = false;
3233

34+
export const openJsonLogging = () => {
35+
console.info('[');
36+
};
37+
38+
export const closeJsonLogging = () => {
39+
console.info(']');
40+
};
41+
42+
export const TEST_RESET_JSON_LOG_FLAG = () => {
43+
IS_PREV_JSON_LOG = false;
44+
};
45+
3346
export const createConsole = (
3447
headMessage: HeadMessage,
35-
type: 'info' | 'error' | 'table' | 'bold' = 'info',
48+
type: 'info' | 'error' | 'table' | 'bold' | 'json' = 'info',
3649
) => {
3750
return <T, U>(...args: T[] | U[]) => {
3851
// print comma if previous log is JSON to separate logs
@@ -57,6 +70,8 @@ export const createConsole = (
5770
return console.info(bigIntStringify({ result: args }));
5871
}
5972
return console.info(getColoredLog(headMessage, args));
73+
case 'json':
74+
return console.info(bigIntStringify(args));
6075
default:
6176
if (program.opts().json) {
6277
return console.info(bigIntStringify({ result: args }));
@@ -70,13 +85,9 @@ export const createConsole = (
7085
};
7186
};
7287

73-
export const TEST_RESET_JSON_LOG_FLAG = () => {
74-
IS_PREV_JSON_LOG = false;
75-
};
76-
7788
const createTable = (headMessage?: HeadMessage) => (args: CreateTableArgs) => {
7889
const { data, params, csvPath } = args;
79-
if (headMessage)
90+
if (headMessage && !program.opts().json)
8091
console.info(`\n${getColoredLog(headMessage, headMessage + ':')}`);
8192

8293
if (!data) return;
@@ -112,3 +123,4 @@ export const logError = createConsole('Error', 'error');
112123
export const logBold = createConsole('Bold', 'bold');
113124
export const logCancel = createConsole('Cancel');
114125
export const logResultSimple = createConsole('Result', 'table');
126+
export const logJson = createConsole('Result', 'json');

0 commit comments

Comments
 (0)