|
1 | 1 | import { ZModelLanguageMetaData } from '@zenstackhq/language'; |
2 | 2 | import colors from 'colors'; |
3 | | -import { Command, Option } from 'commander'; |
| 3 | +import { Command, CommanderError, Option } from 'commander'; |
4 | 4 | import * as actions from './actions'; |
| 5 | +import { CliError } from './cli-error'; |
5 | 6 | import { telemetry } from './telemetry'; |
6 | 7 | import { getVersion } from './utils/version-utils'; |
7 | 8 |
|
@@ -133,22 +134,28 @@ function createProgram() { |
133 | 134 | } |
134 | 135 |
|
135 | 136 | async function main() { |
136 | | - const program = createProgram(); |
137 | | - await telemetry.trackCli(() => void program.parseAsync()); |
138 | | - |
139 | 137 | let exitCode = 0; |
140 | 138 |
|
141 | | - process.on('unhandledRejection', (reason) => { |
142 | | - if (reason instanceof Error) { |
143 | | - telemetry.trackError(reason); |
| 139 | + const program = createProgram(); |
| 140 | + program.exitOverride(); |
| 141 | + |
| 142 | + try { |
| 143 | + await telemetry.trackCli(async () => { |
| 144 | + await program.parseAsync(); |
| 145 | + }); |
| 146 | + } catch (e) { |
| 147 | + if (e instanceof CommanderError) { |
| 148 | + // ignore |
| 149 | + exitCode = e.exitCode; |
| 150 | + } else if (e instanceof CliError) { |
| 151 | + // log |
| 152 | + console.error(colors.red(e.message)); |
| 153 | + exitCode = 1; |
| 154 | + } else { |
| 155 | + console.error(colors.red(`Unhandled error: ${e}`)); |
| 156 | + exitCode = 1; |
144 | 157 | } |
145 | | - exitCode = 1; |
146 | | - }); |
147 | | - |
148 | | - process.on('uncaughtException', (error) => { |
149 | | - telemetry.trackError(error); |
150 | | - exitCode = 1; |
151 | | - }); |
| 158 | + } |
152 | 159 |
|
153 | 160 | if (telemetry.isTracking) { |
154 | 161 | // give telemetry a chance to send events before exit |
|
0 commit comments