Skip to content

fix: fix exit code regression #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/base-commands/base-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ class BaseCommand extends Command {
this.logger.error(error.message);
this.logger.debug(error.stack);
}
this.exit(error.exitCode || 1);
let code = 1;
if (error.exitCode) {
code = parseInt(error.exitCode.toString().substring(0, 2), 10);
}
this.exit(code);
} else {
// System errors
let url = '';
Expand Down
2 changes: 1 addition & 1 deletion src/services/cli-http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class CliRequestClient {
const moreInfoMessage = more_info ? `See ${more_info} for more info.` : '';
const error = {
message: `Error code ${code || 'N/A'} from Twilio: ${message || 'No message provided'}. ${moreInfoMessage}`,
code: code.toString().substring(0, 2),
code,
details,
};

Expand Down
13 changes: 13 additions & 0 deletions test/base-commands/base-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ describe('base-commands', () => {
expect(ctx.stderr).to.contain('oy!');
});

test
.twilioCliEnv()
.stdout()
.do(async (ctx) => {
ctx.testCmd = new BaseCommand(['-o', 'json'], ctx.fakeConfig);
await ctx.testCmd.run();
await ctx.testCmd.catch(new TwilioCliError('oh no', 2003, { errors: [{ message: 'oh no' }] }));
})
.exit(20)
.it('can correctly cut exit code', (ctx) => {
expect(ctx.stdout).to.contain(`"message": "oh no"`);
});

test
.twilioCliEnv()
.stdout()
Expand Down
4 changes: 2 additions & 2 deletions test/base-commands/twilio-client-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ describe('base-commands', () => {
});

setUpTest([], { commandClass: Throwing20003ClientCommand })
.exit(20003)
.exit(20)
.it('should catch access denied errors and enhance the message', (ctx) => {
expect(ctx.stderr).to.contain('Access Denied');
expect(ctx.stderr).to.contain('Standard API Keys');
});

setUpTest([], { commandClass: Throwing20003ClientCommand, envRegion: 'region' })
.exit(20003)
.exit(20)
.it('should catch access denied errors but not enhance the message when using env var auth', (ctx) => {
expect(ctx.stderr).to.contain('Access Denied');
expect(ctx.stderr).to.not.contain('Standard API Keys');
Expand Down
12 changes: 0 additions & 12 deletions test/services/cli-http-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,5 @@ describe('services', () => {
});
expect(response.statusCode).to.equal(200);
});

test.it('correct exit code', () => {
const client = new CliRequestClient('bleh', logger);
const response = {
code: 20404,
message: 'error',
moreInfo: '',
details: '',
};
const { message, code } = client.formatErrorMessage(response);
expect(code).to.equal('20');
});
});
});