Skip to content

Commit ca0f86a

Browse files
committed
Don't swallow errors on varUpdate()
Add MIError class for exceptions. Check error type on varUpdate() failure and rethrow if it's not "Variable object not found".
1 parent aa1ea10 commit ca0f86a

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/backend/backend.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,38 @@ export class VariableObject {
123123
return res;
124124
}
125125
}
126+
127+
// from https://gist.github.com/justmoon/15511f92e5216fa2624b#gistcomment-1928632
128+
export interface MIError extends Error {
129+
readonly name: string;
130+
readonly message: string;
131+
readonly source: string;
132+
};
133+
export interface MIErrorConstructor {
134+
new (message: string, source: string): MIError;
135+
readonly prototype: MIError;
136+
}
137+
138+
export const MIError: MIErrorConstructor = <any>class MIError {
139+
readonly name: string;
140+
readonly message: string;
141+
readonly source: string;
142+
public constructor(message: string, source: string) {
143+
Object.defineProperty(this, 'name', {
144+
get: () => (this.constructor as any).name,
145+
});
146+
Object.defineProperty(this, 'message', {
147+
get: () => message,
148+
});
149+
Object.defineProperty(this, 'source', {
150+
get: () => source,
151+
});
152+
Error.captureStackTrace(this, this.constructor);
153+
}
154+
155+
public toString() {
156+
return `${this.message} (from ${this.source})`;
157+
}
158+
};
159+
Object.setPrototypeOf(MIError as any, Object.create(Error.prototype));
160+
MIError.prototype.constructor = MIError;

src/backend/mi2/mi2.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Breakpoint, IBackend, Stack, SSHArguments, Variable, VariableObject } from "../backend"
1+
import { Breakpoint, IBackend, Stack, SSHArguments, Variable, VariableObject, MIError } from "../backend"
22
import * as ChildProcess from "child_process"
33
import { EventEmitter } from "events"
44
import { parseMI, MINode } from '../mi_parse';
@@ -729,11 +729,11 @@ export class MI2 extends EventEmitter implements IBackend {
729729
this.handlers[sel] = (node: MINode) => {
730730
if (node && node.resultRecords && node.resultRecords.resultClass === "error") {
731731
if (suppressFailure) {
732-
this.log("stderr", "WARNING: Error executing command '" + command + "'");
732+
this.log("stderr", `WARNING: Error executing command '${command}'`);
733733
resolve(node);
734734
}
735735
else
736-
reject((node.result("msg") || "Internal error") + " (from " + command + ")");
736+
reject(new MIError(node.result("msg") || "Internal error", command));
737737
}
738738
else
739739
resolve(node);

src/mibase.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DebugSession, InitializedEvent, TerminatedEvent, StoppedEvent, OutputEvent, Thread, StackFrame, Scope, Source, Handles } from 'vscode-debugadapter';
22
import { DebugProtocol } from 'vscode-debugprotocol';
3-
import { Breakpoint, IBackend, Variable, VariableObject, ValuesFormattingMode } from './backend/backend';
3+
import { Breakpoint, IBackend, Variable, VariableObject, ValuesFormattingMode, MIError } from './backend/backend';
44
import { MINode } from './backend/mi_parse';
55
import { expandValue, isExpandable } from './backend/gdb_expansion';
66
import { MI2 } from './backend/mi2/mi2';
@@ -349,10 +349,15 @@ export class MI2DebugSession extends DebugSession {
349349
varObj = this.variableHandles.get(varId) as any;
350350
}
351351
catch (err) {
352-
varObj = await this.miDebugger.varCreate(variable.name, variable.name);
353-
const varId = findOrCreateVariable(varObj);
354-
varObj.exp = variable.name;
355-
varObj.id = varId;
352+
if (err instanceof MIError && err.message == "Variable object not found") {
353+
varObj = await this.miDebugger.varCreate(variable.name, variable.name);
354+
const varId = findOrCreateVariable(varObj);
355+
varObj.exp = variable.name;
356+
varObj.id = varId;
357+
}
358+
else {
359+
throw err;
360+
}
356361
}
357362
variables.push(varObj.toProtocolVariable());
358363
}

0 commit comments

Comments
 (0)