Skip to content

Commit

Permalink
add errorCallback for when command fails
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLuGit committed Mar 28, 2024
1 parent 9961650 commit a13a956
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type BaseCommandOptions = {
extraOutput?: boolean;
successMessage?: string;
optionalArgs?: (string | undefined)[];
errorCallback?: (error: string) => void;
};
export class BaseCommand {
command: string;
Expand All @@ -47,6 +48,7 @@ export class BaseCommand {
exited: boolean = false;
extraOutput?: string[];
progressWindow: BackgroundProgress;
errorCallback?: (error: string) => void;

constructor(options: BaseCommandOptions) {
// the constructor is what is called whenever a new instance of the class is created
Expand Down Expand Up @@ -89,6 +91,7 @@ export class BaseCommand {
this.requiresProsProject = options.requiresProsProject;
this.extraOutput = options.extraOutput ? [] : undefined;
this.successMessage = options.successMessage;
this.errorCallback = options.errorCallback;

this.progressWindow = new BackgroundProgress(this.message, true, false);
// As far as implementing this onto each command, there are two ways you can do this.
Expand Down Expand Up @@ -180,6 +183,9 @@ export class BaseCommand {
child.stdout.on("data", (data) => {
this.parseOutput(data.toString().split(/\r?\n/), child).catch((e) => {
hasError = true;
if (this.errorCallback) {
this.errorCallback(e);
}
vscode.window.showErrorMessage(e, "View Output").then((response) => {
if (response) {
output.show();
Expand All @@ -190,6 +196,9 @@ export class BaseCommand {
child.stderr.on("data", (data) => {
this.parseOutput(data.toString().split(/\r?\n/), child).catch((e) => {
hasError = true;
if (this.errorCallback) {
this.errorCallback(e);
}
vscode.window.showErrorMessage(e, "View Output").then((response) => {
if (response) {
output.show();
Expand Down
9 changes: 7 additions & 2 deletions src/commands/buildUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ import { window } from "vscode";
import { upload } from "./upload";

export const buildUpload = async () => {
var buildFailed = false;
const buildCommandOptions: BaseCommandOptions = {
command: "pros",
args: ["make"],
message: "Building Project",
requiresProsProject: true,
successMessage: "hidden",
errorCallback: () => {
buildFailed = true;
},
};
const buildCommand: BaseCommand = new BaseCommand(buildCommandOptions);
try {
await buildCommand.runCommand();
} catch (err: any) {
await window.showErrorMessage(err.message);
return;
}
await upload();
if (!buildFailed) {
await upload();
}
};

0 comments on commit a13a956

Please sign in to comment.