Skip to content
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
2 changes: 1 addition & 1 deletion packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('CliRepl', () => {

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

Expand Down
3 changes: 3 additions & 0 deletions packages/cli-repl/src/format-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type FormatOptions = {
depth?: number;
maxArrayLength?: number;
maxStringLength?: number;
showStackTraces?: boolean;
};

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

return result;
Expand Down
16 changes: 16 additions & 0 deletions packages/cli-repl/src/mongosh-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,22 @@ describe('MongoshNodeRepl', () => {
const { history } = mongoshRepl.runtimeState().repl as any;
expect(history).to.have.lengthOf(2);
});

it('controls stack trace display', async() => {
output = '';
input.write('throw new Error("yellow")\n');
await waitEval(bus);
expect(stripAnsi(output)).to.match(/Error: yellow\n(> )+$/);

input.write('config.set("showStackTraces", true)\n');
await waitEval(bus);
expect(output).to.include('Setting "showStackTraces" has been changed');

output = '';
input.write('throw new Error("orange")\n');
await waitEval(bus);
expect(stripAnsi(output)).to.match(/Error: orange\n +at\b/);
});
});

it('refreshes the prompt if a window resize occurs', async() => {
Expand Down
10 changes: 8 additions & 2 deletions packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class MongoshNodeRepl implements EvaluationListener {
insideAutoComplete: boolean;
inspectDepth = 0;
started = false;
showStackTraces = false;

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

this.inspectDepth = await this.getConfig('inspectDepth');
this.showStackTraces = await this.getConfig('showStackTraces');

const repl = asyncRepl.start({
start: prettyRepl.start,
Expand Down Expand Up @@ -414,12 +416,13 @@ class MongoshNodeRepl implements EvaluationListener {
return clr(text, style, this.getFormatOptions());
}

getFormatOptions(): { colors: boolean, depth: number } {
getFormatOptions(): { colors: boolean, depth: number, showStackTraces: boolean } {
const output = this.output as WriteStream;
return {
colors: this._runtimeState?.repl?.useColors ??
(output.isTTY && output.getColorDepth() > 1),
depth: this.inspectDepth
depth: this.inspectDepth,
showStackTraces: this.showStackTraces
};
}

Expand Down Expand Up @@ -458,6 +461,9 @@ class MongoshNodeRepl implements EvaluationListener {
if (key === 'inspectDepth') {
this.inspectDepth = +value;
}
if (key === 'showStackTraces') {
this.showStackTraces = !!value;
}
return this.ioProvider.setConfig(key, value);
}

Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class CliUserConfig extends ShellUserConfig {
disableGreetingMessage = false;
inspectDepth = 6;
historyLength = 1000;
showStackTraces = false;
}

export interface ConfigProvider<T> {
Expand Down