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

Include stack trace in errors, Closes #5660 #5758

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
49 changes: 48 additions & 1 deletion src/Command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ class MockCommand4 extends Command {
}
}

class MockCommand5 extends Command {
public get name(): string {
return 'Mock command 5 [opt]';
}

public get description(): string {
return 'Mock command 5 description';
}

public async commandAction(): Promise<void> {
}

public handlePromiseError(response: any): void {
this.handleRejectedODataPromise(response);
}
}

describe('Command', () => {
let telemetryCommandName: any;
let logger: Logger;
Expand Down Expand Up @@ -156,7 +173,8 @@ describe('Command', () => {
sinonUtil.restore([
process.exit,
accessToken.isAppOnlyAccessToken,
accessToken.getUserIdFromAccessToken
accessToken.getUserIdFromAccessToken,
cli.optionsFromArgs
]);
auth.service.connected = false;
});
Expand Down Expand Up @@ -602,4 +620,33 @@ describe('Command', () => {

await assert.rejects(command.action(logger, { options: { option1: '@meUsername' } }), new CommandError(`It's not possible to use @meUsername with application permissions`));
});

it('correctly handles stack trace error when using debug for oData json error handling', async () => {
try {
sinon.stub(cli, 'error');
const mock = new MockCommand2();
sinon.stub(cli, 'optionsFromArgs').value({ options: { debug: true } });
mock.handlePromiseError({
error: { error_description: 'abc' }
});
assert.fail('No exception was thrown.');
}
catch (err: any) {
assert.strictEqual(JSON.stringify(err), JSON.stringify(new CommandError('abc')));
}
});

it('correctly handles stack trace error when using debug for oData error handling', async () => {
try {
const mock = new MockCommand5();
sinon.stub(cli, 'optionsFromArgs').value({ options: { debug: true } });
mock.handlePromiseError({
error: { error_description: 'abc' }
});
assert.fail('No exception was thrown.');
}
catch (err: any) {
assert.strictEqual(JSON.stringify(err), JSON.stringify({ 'message': { 'error_description': 'abc' } }));
}
});
});
12 changes: 12 additions & 0 deletions src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ export default abstract class Command {
}

protected handleRejectedODataPromise(res: any): void {
/* c8 ignore next 4 */
if (this.debug && typeof global.it === 'undefined') {
const error = new Error();
cli.error(error.stack);
}

if (res.error) {
try {
const err: ODataError = JSON.parse(res.error);
Expand Down Expand Up @@ -373,6 +379,12 @@ export default abstract class Command {
}

protected handleRejectedODataJsonPromise(response: any): void {
/* c8 ignore next 4 */
if (this.debug && typeof global.it === 'undefined') {
const error = new Error();
cli.error(error.stack);
}

if (response.error &&
response.error['odata.error'] &&
response.error['odata.error'].message) {
Expand Down
Loading