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

Add helpful error messages related to serial monitor api #1618

Merged
merged 3 commits into from
Mar 8, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 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,16 @@ 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) {
Logger.error(errorString)
if (showError) {
vscode.window.showErrorMessage(errorString);
}
}
}

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