Skip to content

Commit

Permalink
fix(cli-repl): improve debuggability of mongosh not initialized yet
Browse files Browse the repository at this point in the history
… errors

These errors have historically been very hard to debug, so adding stack
traces unconditionally will hopefully improve this situation in the long term.
  • Loading branch information
addaleax committed Dec 12, 2024
1 parent f9a0631 commit 93761ce
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type Mutable<T> = {
*/
class MongoshNodeRepl implements EvaluationListener {
_runtimeState: MongoshRuntimeState | null;
closeTrace?: string;
input: Readable;
lineByLineInput: LineByLineInput;
output: Writable;
Expand Down Expand Up @@ -1028,7 +1029,13 @@ class MongoshNodeRepl implements EvaluationListener {
*/
runtimeState(): MongoshRuntimeState {
if (this._runtimeState === null) {
throw new MongoshInternalError('Mongosh not initialized yet');
// This error can be really hard to debug, so we always attach stack traces
// from both when .close() was called and when
throw new MongoshInternalError(
`mongosh not initialized yet\nCurrentTrace: ${
new Error().stack
}\nClose trace: ${this.closeTrace}\n`
);
}
return this._runtimeState;
}
Expand All @@ -1043,6 +1050,7 @@ class MongoshNodeRepl implements EvaluationListener {
const rs = this._runtimeState;
if (rs) {
this._runtimeState = null;
this.closeTrace = new Error().stack;
rs.repl?.close();
await rs.instanceState.close(true);
await new Promise((resolve) => this.output.write('', resolve));
Expand Down

0 comments on commit 93761ce

Please sign in to comment.