Skip to content

Commit fed2e87

Browse files
authored
feat(cli-repl): add showStackTraces option MONGOSH-601 (#819)
1 parent 7d18402 commit fed2e87

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('CliRepl', () => {
157157

158158
it('returns the list of available config options when asked to', () => {
159159
expect(cliRepl.listConfigOptions()).to.deep.equal([
160-
'batchSize', 'enableTelemetry', 'inspectDepth', 'historyLength'
160+
'batchSize', 'enableTelemetry', 'inspectDepth', 'historyLength', 'showStackTraces'
161161
]);
162162
});
163163

packages/cli-repl/src/format-output.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type FormatOptions = {
1818
depth?: number;
1919
maxArrayLength?: number;
2020
maxStringLength?: number;
21+
showStackTraces?: boolean;
2122
};
2223

2324
/**
@@ -167,6 +168,8 @@ export function formatError(error: Error, options: FormatOptions): string {
167168
}
168169
// leave a bit of breathing room after the syntax error message output
169170
result += '\n\n';
171+
} else if (options.showStackTraces && error.stack) {
172+
result += error.stack.slice(error.stack.indexOf('\n'));
170173
}
171174

172175
return result;

packages/cli-repl/src/mongosh-repl.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,22 @@ describe('MongoshNodeRepl', () => {
463463
const { history } = mongoshRepl.runtimeState().repl as any;
464464
expect(history).to.have.lengthOf(2);
465465
});
466+
467+
it('controls stack trace display', async() => {
468+
output = '';
469+
input.write('throw new Error("yellow")\n');
470+
await waitEval(bus);
471+
expect(stripAnsi(output)).to.match(/Error: yellow\n(> )+$/);
472+
473+
input.write('config.set("showStackTraces", true)\n');
474+
await waitEval(bus);
475+
expect(output).to.include('Setting "showStackTraces" has been changed');
476+
477+
output = '';
478+
input.write('throw new Error("orange")\n');
479+
await waitEval(bus);
480+
expect(stripAnsi(output)).to.match(/Error: orange\n +at\b/);
481+
});
466482
});
467483

468484
it('refreshes the prompt if a window resize occurs', async() => {

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class MongoshNodeRepl implements EvaluationListener {
7171
insideAutoComplete: boolean;
7272
inspectDepth = 0;
7373
started = false;
74+
showStackTraces = false;
7475

7576
constructor(options: MongoshNodeReplOptions) {
7677
this.input = options.input;
@@ -95,6 +96,7 @@ class MongoshNodeRepl implements EvaluationListener {
9596
await this.printStartupLog(internalState);
9697

9798
this.inspectDepth = await this.getConfig('inspectDepth');
99+
this.showStackTraces = await this.getConfig('showStackTraces');
98100

99101
const repl = asyncRepl.start({
100102
start: prettyRepl.start,
@@ -414,12 +416,13 @@ class MongoshNodeRepl implements EvaluationListener {
414416
return clr(text, style, this.getFormatOptions());
415417
}
416418

417-
getFormatOptions(): { colors: boolean, depth: number } {
419+
getFormatOptions(): { colors: boolean, depth: number, showStackTraces: boolean } {
418420
const output = this.output as WriteStream;
419421
return {
420422
colors: this._runtimeState?.repl?.useColors ??
421423
(output.isTTY && output.getColorDepth() > 1),
422-
depth: this.inspectDepth
424+
depth: this.inspectDepth,
425+
showStackTraces: this.showStackTraces
423426
};
424427
}
425428

@@ -458,6 +461,9 @@ class MongoshNodeRepl implements EvaluationListener {
458461
if (key === 'inspectDepth') {
459462
this.inspectDepth = +value;
460463
}
464+
if (key === 'showStackTraces') {
465+
this.showStackTraces = !!value;
466+
}
461467
return this.ioProvider.setConfig(key, value);
462468
}
463469

packages/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export class CliUserConfig extends ShellUserConfig {
116116
disableGreetingMessage = false;
117117
inspectDepth = 6;
118118
historyLength = 1000;
119+
showStackTraces = false;
119120
}
120121

121122
export interface ConfigProvider<T> {

0 commit comments

Comments
 (0)