Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1618 from microsoft/dev/gcampbell/OutputLoggerMes…
Browse files Browse the repository at this point in the history
…sage

Add helpful error messages related to serial monitor api
  • Loading branch information
gcampbell-msft authored Mar 8, 2023
2 parents 5f2cf62 + 832bafb commit 78209ee
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/serialmonitor/serialMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ export class SerialMonitor implements vscode.Disposable {
});

this.serialMonitorApi = await getSerialMonitorApi(Version.latest, extensionContext);

this.checkForUndefinedSerialMonitorApi();
}

public get initialized(): boolean {
return !!this.extensionContext;
}

public async selectSerialPort(): Promise<string | undefined> {
this.checkForUndefinedSerialMonitorApi(true);

const ports = await this.serialMonitorApi.listAvailablePorts();
if (!ports.length) {
vscode.window.showInformationMessage("No serial port is available.");
Expand Down Expand Up @@ -113,6 +117,8 @@ export class SerialMonitor implements vscode.Disposable {
}

public async openSerialMonitor(restore: boolean = false): Promise<void> {
this.checkForUndefinedSerialMonitorApi(true);

if (!this.currentPort) {
const ans = await vscode.window.showInformationMessage("No serial port was selected, please select a serial port first", "Select", "Cancel");
if (ans === "Select") {
Expand Down Expand Up @@ -151,6 +157,8 @@ export class SerialMonitor implements vscode.Disposable {
}

public async closeSerialMonitor(port?: string): Promise<boolean> {
this.checkForUndefinedSerialMonitorApi(true);

const portToClose = port ?? this.currentPort;
let closed = false;
if (portToClose) {
Expand All @@ -165,6 +173,17 @@ export class SerialMonitor implements vscode.Disposable {
this.serialMonitorApi.dispose();
}

private checkForUndefinedSerialMonitorApi(showError: boolean = false): void {
const errorString = "Serial Monitor API was not retrieved. You may not have the most recent version of the Serial Monitor extension installed.";
if (this.serialMonitorApi === undefined) {
if (showError) {
Logger.notifyUserError("UndefinedSerialMonitorApi", new Error(errorString));
} else {
Logger.traceError("UndefinedSerialMonitorApi", new Error(errorString));
}
}
}

private updatePortListStatus(port?: string) {
const dc = DeviceContext.getInstance();
if (port) {
Expand Down

0 comments on commit 78209ee

Please sign in to comment.