Skip to content

Commit d790fd0

Browse files
authored
fix(cli-repl): improve debuggability of mongosh not initialized yeterrors MONGOSH-1943 (#2299)
These errors have historically been very hard to debug, so adding stack traces unconditionally will hopefully improve this situation in the long term.
1 parent 8d8ac6f commit d790fd0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('MongoshNodeRepl', function () {
121121

122122
it('throws an error if internal methods are used too early', function () {
123123
expect(() => mongoshRepl.runtimeState()).to.throw(
124-
'Mongosh not initialized yet'
124+
/mongosh not initialized yet\nCurrent trace/
125125
);
126126
});
127127

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ type Mutable<T> = {
126126
*/
127127
class MongoshNodeRepl implements EvaluationListener {
128128
_runtimeState: MongoshRuntimeState | null;
129+
closeTrace?: string;
129130
input: Readable;
130131
lineByLineInput: LineByLineInput;
131132
output: Writable;
@@ -1028,7 +1029,13 @@ class MongoshNodeRepl implements EvaluationListener {
10281029
*/
10291030
runtimeState(): MongoshRuntimeState {
10301031
if (this._runtimeState === null) {
1031-
throw new MongoshInternalError('Mongosh not initialized yet');
1032+
// This error can be really hard to debug, so we always attach stack traces
1033+
// from both when .close() was called and when
1034+
throw new MongoshInternalError(
1035+
`mongosh not initialized yet\nCurrent trace: ${
1036+
new Error().stack
1037+
}\nClose trace: ${this.closeTrace}\n`
1038+
);
10321039
}
10331040
return this._runtimeState;
10341041
}
@@ -1043,6 +1050,7 @@ class MongoshNodeRepl implements EvaluationListener {
10431050
const rs = this._runtimeState;
10441051
if (rs) {
10451052
this._runtimeState = null;
1053+
this.closeTrace = new Error().stack;
10461054
rs.repl?.close();
10471055
await rs.instanceState.close(true);
10481056
await new Promise((resolve) => this.output.write('', resolve));

0 commit comments

Comments
 (0)