Skip to content

Commit 212838e

Browse files
CopilotB4nan
andcommitted
Fix error handling to exit with code 1 on command errors
Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
1 parent 05d4614 commit 212838e

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/lib/command-framework/apify-command.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,17 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
301301

302302
if (missingRequiredArgs.size) {
303303
this._printMissingRequiredArgs(missingRequiredArgs);
304-
return;
305304
}
306305

307306
this._parseFlags(rawFlags, rawTokens);
308307

308+
let hadError: Error | null = null;
309+
309310
try {
310311
await this.run();
311312
} catch (err: any) {
312313
error({ message: err.message });
314+
hadError = err;
313315
} finally {
314316
// analytics
315317
if (!this.telemetryData.actorLanguage && COMMANDS_WITHIN_ACTOR.includes(this.commandString)) {
@@ -336,6 +338,15 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
336338
this.telemetryData,
337339
);
338340
}
341+
342+
if (hadError) {
343+
// In test mode (skipTelemetry=true), throw the error so tests can catch it
344+
// In normal mode, exit with code 1
345+
if (this.skipTelemetry) {
346+
throw hadError;
347+
}
348+
process.exit(1);
349+
}
339350
}
340351
}
341352

@@ -588,7 +599,7 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
588599
}
589600
}
590601

591-
private _printMissingRequiredArgs(missingRequiredArgs: Map<string, TaggedArgBuilder<ArgTag, unknown>>) {
602+
private _printMissingRequiredArgs(missingRequiredArgs: Map<string, TaggedArgBuilder<ArgTag, unknown>>): never {
592603
const help = selectiveRenderHelpForCommand(this.ctor, {
593604
showUsageString: true,
594605
});
@@ -616,6 +627,8 @@ export abstract class ApifyCommand<T extends typeof BuiltApifyCommand = typeof B
616627
help,
617628
].join('\n'),
618629
});
630+
631+
process.exit(1);
619632
}
620633

621634
private _handleStdin(mode: StdinMode) {

test/local/commands/secrets/add.test.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ describe('apify secrets add', () => {
1616
}
1717
});
1818

19+
afterEach(async () => {
20+
// Clean up after each test
21+
const secrets = getSecretsFile();
22+
if (secrets[SECRET_KEY]) {
23+
await testRunCommand(SecretsRmCommand, {
24+
args_name: SECRET_KEY,
25+
});
26+
}
27+
});
28+
1929
it('should work', async () => {
2030
await testRunCommand(SecretsAddCommand, {
2131
args_name: SECRET_KEY,
@@ -26,13 +36,19 @@ describe('apify secrets add', () => {
2636
expect(secrets[SECRET_KEY]).to.eql(SECRET_VALUE);
2737
});
2838

29-
afterAll(async () => {
30-
const secrets = getSecretsFile();
39+
it('should throw error when adding duplicate secret', async () => {
40+
// First add a secret
41+
await testRunCommand(SecretsAddCommand, {
42+
args_name: SECRET_KEY,
43+
args_value: SECRET_VALUE,
44+
});
3145

32-
if (secrets[SECRET_KEY]) {
33-
await testRunCommand(SecretsRmCommand, {
46+
// Try to add the same secret again and expect it to throw
47+
await expect(
48+
testRunCommand(SecretsAddCommand, {
3449
args_name: SECRET_KEY,
35-
});
36-
}
50+
args_value: SECRET_VALUE,
51+
}),
52+
).rejects.toThrow(`Secret with name ${SECRET_KEY} already exists`);
3753
});
3854
});

0 commit comments

Comments
 (0)