Skip to content
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

feat: Support detailed error objects in cli #108

Merged
merged 9 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"columnify": "^1.5.4",
"fs-extra": "^9.0.1",
"inquirer": "^7.3.0",
"keytar": "^6.0.1",
alecnicolas marked this conversation as resolved.
Show resolved Hide resolved
"qs": "^6.9.4",
"semver": "^7.3.2",
"tsv": "^0.2.0",
Expand Down
8 changes: 6 additions & 2 deletions src/base-commands/base-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ class BaseCommand extends Command {

if (instanceOf(error, TwilioCliError)) {
// User/API errors
this.logger.error(error.message);
this.logger.debug(error.stack);
if (this.flags['cli-output-format'] === 'json') {
alecnicolas marked this conversation as resolved.
Show resolved Hide resolved
this.output(error);
} else {
this.logger.error(error.message);
this.logger.debug(error.stack);
}
this.exit(error.exitCode || 1);
} else {
// System errors
Expand Down
9 changes: 6 additions & 3 deletions src/services/cli-http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,16 @@ class CliRequestClient {
this.logger.debug(`response.statusCode: ${response.status}`);
this.logger.debug(`response.headers: ${JSON.stringify(response.headers)}`);

/* eslint-disable camelcase */
if (response.status < 200 || response.status >= 400) {
const parsed = response.data;
const { code, message, more_info, details } = response.data;
alecnicolas marked this conversation as resolved.
Show resolved Hide resolved
throw new TwilioCliError(
`Error code ${parsed.code} from Twilio: ${parsed.message}. See ${parsed.more_info} for more info.`,
parsed.code,
`Error code ${code} from Twilio: ${message}. See ${more_info} for more info.`,
code,
details,
);
}
/* eslint-enable camelcase */

return {
body: response.data,
Expand Down
3 changes: 2 additions & 1 deletion src/services/error.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class TwilioCliError extends Error {
constructor(message, exitCode) {
constructor(message, exitCode, details) {
alecnicolas marked this conversation as resolved.
Show resolved Hide resolved
super(message);
this.name = this.constructor.name;
this.exitCode = exitCode;
this.details = 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', 1, { errors: [{ message: 'oh no' }] }));
})
.exit(1)
.it('can correctly display error details', (ctx) => {
expect(ctx.stdout).to.contain(`"message": "oh no"`);
thinkingserious marked this conversation as resolved.
Show resolved Hide resolved
});

test
.twilioCliEnv()
.do((ctx) => {
Expand Down