Skip to content

Commit 95698f4

Browse files
committed
fix: proper error handling in CLI
1 parent a75d5b2 commit 95698f4

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

packages/cli/src/brownfield/commands/packageIos.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const packageIosCommand = curryOptions(
4747

4848
options.buildFolder ??= path.join(brownieCacheDir, 'build');
4949

50-
packageIosAction(
50+
await packageIosAction(
5151
options,
5252
{
5353
projectRoot,

packages/cli/src/shared/utils/cli.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logger, type RockCLIOptions } from '@rock-js/tools';
1+
import { logger, RockError, type RockCLIOptions } from '@rock-js/tools';
22

33
import type { Command } from 'commander';
44

@@ -23,11 +23,25 @@ export function curryOptions(programCommand: Command, options: RockCLIOptions) {
2323
return programCommand;
2424
}
2525

26-
function handleActionError(error: Error) {
27-
logger.error(`Error running command: ${error.message}`);
28-
process.exit(1);
29-
}
30-
3126
export function actionRunner<T, R>(fn: (...args: T[]) => Promise<R>) {
32-
return (...args: T[]) => fn(...args).catch(handleActionError);
27+
return async function wrappedCLIAction(...args: T[]) {
28+
try {
29+
await fn(...args);
30+
} catch (error) {
31+
if (error instanceof RockError) {
32+
if (logger.isVerbose()) {
33+
logger.error(error);
34+
} else {
35+
logger.error(error.message);
36+
if (error.cause) {
37+
logger.error(`Cause: ${error.cause}`);
38+
}
39+
}
40+
} else {
41+
logger.error(`Unexpected error while running command:`, error);
42+
}
43+
44+
process.exit(1);
45+
}
46+
};
3347
}

0 commit comments

Comments
 (0)