Skip to content

Commit

Permalink
chore: Improve handling errors at restarting workspace
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Nikitenko <[email protected]>
  • Loading branch information
RomanNikitenko committed Mar 8, 2023
1 parent 7892768 commit 5d1e16e
Showing 1 changed file with 34 additions and 35 deletions.
69 changes: 34 additions & 35 deletions code/extensions/che-remote/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

context.subscriptions.push(
vscode.commands.registerCommand('che-remote.command.restartFromLocalDevfile', async () => {
await updateDevfile(cheApi);
await vscode.commands.executeCommand('che-remote.command.restartWorkspace');
try {
await updateDevfile(cheApi);
await vscode.commands.executeCommand('che-remote.command.restartWorkspace');
} catch (error) {
console.error(`Something went wrong for the 'Restart From Local Devfile' action: ${error}`);
vscode.window.showErrorMessage(`Can not restart the workspace from the local devfile: ${error}`);
}
})
);
}
Expand All @@ -76,38 +81,32 @@ async function updateDevfile(cheApi: any): Promise<void> {
} = cheApi.getDevfileService();
const devWorkspaceGenerator = new DevWorkspaceGenerator();

try {
const projectPath = process.env.PROJECT_SOURCE
let devfilePath: string | undefined = `${projectPath}/${DEVFILE_NAME}`;

let devfileExists = await fs.pathExists(devfilePath);
if (!devfileExists) {
devfilePath = `${projectPath}/${DOT_DEVFILE_NAME}`;
devfileExists = await fs.pathExists(devfilePath);
}

if (!devfileExists) {
devfilePath = await vscode.window.showInputBox({ title: 'Path to the devfile', value: `${projectPath}/` });
devfileExists = devfilePath ? await fs.pathExists(devfilePath) : false;
}

if (!devfileExists) {
vscode.window.showWarningMessage(`Can not restart the workspace - the devfile was not found by path: ${devfilePath}`);
return undefined;
}

const currentDevfile = await devfileService.get()
const projects = currentDevfile.projects || [];

const newContent = await devWorkspaceGenerator.generateDevfileContext({ devfilePath, editorEntry: DEFAULT_EDITOR_ENTRY, projects: [] }, axios.default);
if (newContent) {
newContent.devWorkspace.spec!.template!.projects = projects;
await devfileService.updateDevfile(newContent.devWorkspace.spec?.template);
} else {
throw new Error('An error occurred while performing generation a new devfile context');
}
} catch (error) {
console.error(`Something went wrong for the 'Restart From Local Devfile' action: ${error}`);
vscode.window.showErrorMessage('Can not restart the workspace from the local devfile');
const projectPath = process.env.PROJECT_SOURCE
let devfilePath: string | undefined = `${projectPath}/${DEVFILE_NAME}`;

let devfileExists = await fs.pathExists(devfilePath);
if (!devfileExists) {
devfilePath = `${projectPath}/${DOT_DEVFILE_NAME}`;
devfileExists = await fs.pathExists(devfilePath);
}

if (!devfileExists) {
devfilePath = await vscode.window.showInputBox({ title: 'Path to the devfile', value: `${projectPath}/` });
devfileExists = devfilePath ? await fs.pathExists(devfilePath) : false;
}

if (!devfileExists) {
throw new Error(`The devfile was not found by path: ${devfilePath}`);
}

const currentDevfile = await devfileService.get()
const projects = currentDevfile.projects || [];

const newContent = await devWorkspaceGenerator.generateDevfileContext({ devfilePath, editorEntry: DEFAULT_EDITOR_ENTRY, projects: [] }, axios.default);
if (newContent) {
newContent.devWorkspace.spec!.template!.projects = projects;
await devfileService.updateDevfile(newContent.devWorkspace.spec?.template);
} else {
throw new Error('An error occurred while performing generation a new devfile context');
}
}

0 comments on commit 5d1e16e

Please sign in to comment.