Skip to content

Commit 93761ce

Browse files
committed
fix(cli-repl): improve debuggability of mongosh not initialized yet errors
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 f9a0631 commit 93761ce

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

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\nCurrentTrace: ${
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)