Skip to content

Commit

Permalink
feat: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjenek committed Oct 3, 2024
1 parent 7d3e6cd commit 1e3a97b
Show file tree
Hide file tree
Showing 21 changed files with 243 additions and 137 deletions.
15 changes: 3 additions & 12 deletions src/controllers/DisplayResourceDependencyGraphController.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import * as vscode from 'vscode';
import { IResourceDependencyGraphService } from '../services/ResourcesGraphService';
import { isHumanitecExtensionError } from '../errors/IHumanitecExtensionError';
import { ILoggerService } from '../services/LoggerService';
import { IErrorHandlerService } from '../services/ErrorHandlerService';

export class DisplayResourcesGraphController {
private constructor() {}

static register(
context: vscode.ExtensionContext,
resourcesGraphService: IResourceDependencyGraphService,
logger: ILoggerService
errorHandler: IErrorHandlerService
) {
const disposable = vscode.commands.registerCommand(
'humanitec.display_resources_graph',
Expand All @@ -27,15 +26,7 @@ export class DisplayResourcesGraphController {
);
panel.webview.html = this.getWebviewContent(graph);
} catch (error) {
if (isHumanitecExtensionError(error)) {
vscode.window.showErrorMessage(error.message());
logger.error(error.details());
} else {
logger.error(JSON.stringify({ error }));
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
}
errorHandler.handle(error);
}
}
);
Expand Down
41 changes: 10 additions & 31 deletions src/controllers/HumanitecSidebarController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
ResourceTypeTreeItem,
} from '../providers/AvailableResourceTypesProvider';
import { IResourceTypeRepository } from '../repos/ResourceTypeRepository';
import { ILoggerService } from '../services/LoggerService';
import { isHumanitecExtensionError } from '../errors/IHumanitecExtensionError';
import {
OrganizationStructureItem,
OrganizationStructureProvider,
Expand All @@ -18,6 +16,7 @@ import { Organization } from '../domain/Organization';
import { ConfigKey } from '../domain/ConfigKey';
import { Application } from '../domain/Application';
import { ControllerIsAlreadyRegisteredError } from '../errors/ControllerIsAlreadyRegisteredError';
import { IErrorHandlerService } from '../services/ErrorHandlerService';

export class HumanitecSidebarController {
private static instance: HumanitecSidebarController;
Expand All @@ -34,7 +33,7 @@ export class HumanitecSidebarController {
applicationRepository: IApplicationRepository,
environmentRepository: IEnvironmentRepository,
configurationRepository: IConfigurationRepository,
logger: ILoggerService
errorHandler: IErrorHandlerService
) {
if (this.instance !== undefined) {
throw new ControllerIsAlreadyRegisteredError(
Expand All @@ -43,13 +42,15 @@ export class HumanitecSidebarController {
}

const availableResourceTypesProvider = new AvailableResourceTypesProvider(
resourceTypeRepository
resourceTypeRepository,
errorHandler
);
const organizationStructureProvider = new OrganizationStructureProvider(
organizationRepository,
applicationRepository,
environmentRepository,
configurationRepository
configurationRepository,
errorHandler
);
this.instance = new HumanitecSidebarController(
availableResourceTypesProvider,
Expand All @@ -72,15 +73,7 @@ export class HumanitecSidebarController {
try {
availableResourceTypesProvider.refresh();
} catch (error) {
if (isHumanitecExtensionError(error)) {
logger.error(error.details());
vscode.window.showErrorMessage(error.message());
} else {
logger.error(JSON.stringify({ error }));
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
}
errorHandler.handle(error);
}
}
);
Expand All @@ -92,15 +85,7 @@ export class HumanitecSidebarController {
try {
organizationStructureProvider.refresh();
} catch (error) {
if (isHumanitecExtensionError(error)) {
logger.error(error.details());
vscode.window.showErrorMessage(error.message());
} else {
logger.error(JSON.stringify({ error }));
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
}
errorHandler.handle(error);
}
}
);
Expand All @@ -121,10 +106,7 @@ export class HumanitecSidebarController {
}
vscode.env.openExternal(url);
} catch (error) {
logger.error(JSON.stringify({ error }));
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
errorHandler.handle(error);
}
}
);
Expand Down Expand Up @@ -179,10 +161,7 @@ export class HumanitecSidebarController {
await availableResourceTypesProvider.refresh();
await organizationStructureProvider.refresh();
} catch (error) {
logger.error(JSON.stringify({ error }));
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
errorHandler.handle(error);
}
}
);
Expand Down
15 changes: 3 additions & 12 deletions src/controllers/InitializeScoreFileController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as vscode from 'vscode';
import { IScoreInitializationService } from '../services/ScoreInitializationService';
import { ILoggerService } from '../services/LoggerService';
import { isHumanitecExtensionError } from '../errors/IHumanitecExtensionError';
import { IErrorHandlerService } from '../services/ErrorHandlerService';

export class InitializeScoreFileController {
private constructor() {}
Expand All @@ -10,7 +9,7 @@ export class InitializeScoreFileController {
context: vscode.ExtensionContext,
initializationService: IScoreInitializationService,
enable: boolean,
logger: ILoggerService
errorHandler: IErrorHandlerService
) {
const disposable = vscode.commands.registerCommand(
'humanitec.score.init',
Expand Down Expand Up @@ -43,15 +42,7 @@ export class InitializeScoreFileController {
vscode.window.showInformationMessage('No workspace is opened');
}
} catch (error) {
if (isHumanitecExtensionError(error)) {
logger.error(error.details());
vscode.window.showErrorMessage(error.message());
} else {
logger.error(JSON.stringify({ error }));
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
}
errorHandler.handle(error);
}
}
);
Expand Down
15 changes: 3 additions & 12 deletions src/controllers/LoginController.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as vscode from 'vscode';
import { ISecretRepository } from '../repos/SecretRepository';
import { SecretKey } from '../domain/SecretKey';
import { ILoggerService } from '../services/LoggerService';
import { ILoginService } from '../services/LoginService';
import { isHumanitecExtensionError } from '../errors/IHumanitecExtensionError';
import { IErrorHandlerService } from '../services/ErrorHandlerService';

export class LoginController {
private constructor() {}
Expand All @@ -12,7 +11,7 @@ export class LoginController {
context: vscode.ExtensionContext,
loginService: ILoginService,
secrets: ISecretRepository,
logger: ILoggerService
errorHandler: IErrorHandlerService
) {
const disposable = vscode.commands.registerCommand(
'humanitec.login',
Expand Down Expand Up @@ -51,15 +50,7 @@ export class LoginController {
'Humanitec extension successfully configured!'
);
} catch (error) {
if (isHumanitecExtensionError(error)) {
vscode.window.showErrorMessage(error.message());
logger.error(error.details());
} else {
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
logger.error(JSON.stringify({ error }));
}
errorHandler.handle(error);
}
}
);
Expand Down
15 changes: 3 additions & 12 deletions src/controllers/OpenConfiguredTerminalController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { IConfigurationRepository } from '../repos/ConfigurationRepository';
import { ISecretRepository } from '../repos/SecretRepository';
import { SecretKey } from '../domain/SecretKey';
import { ConfigKey } from '../domain/ConfigKey';
import { isHumanitecExtensionError } from '../errors/IHumanitecExtensionError';
import { ILoggerService } from '../services/LoggerService';
import { IErrorHandlerService } from '../services/ErrorHandlerService';

export class OpenConfiguredTerminalController {
private constructor() {}
Expand All @@ -13,7 +12,7 @@ export class OpenConfiguredTerminalController {
context: vscode.ExtensionContext,
configs: IConfigurationRepository,
secrets: ISecretRepository,
logger: ILoggerService
errorHandler: IErrorHandlerService
) {
const disposable = vscode.commands.registerCommand(
'humanitec.open_configured_terminal',
Expand All @@ -38,15 +37,7 @@ export class OpenConfiguredTerminalController {

terminal.show();
} catch (error) {
if (isHumanitecExtensionError(error)) {
logger.error(error.details());
vscode.window.showErrorMessage(error.message());
} else {
logger.error(JSON.stringify({ error }));
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
}
errorHandler.handle(error);
}
}
);
Expand Down
9 changes: 3 additions & 6 deletions src/controllers/SetTokenController.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as vscode from 'vscode';
import { ISecretRepository } from '../repos/SecretRepository';
import { SecretKey } from '../domain/SecretKey';
import { ILoggerService } from '../services/LoggerService';
import { IErrorHandlerService } from '../services/ErrorHandlerService';

export class SetTokenController {
private constructor() {}

static register(
context: vscode.ExtensionContext,
secrets: ISecretRepository,
logger: ILoggerService
errorHandler: IErrorHandlerService
) {
const disposable = vscode.commands.registerCommand(
'humanitec.set_token',
Expand All @@ -35,10 +35,7 @@ export class SetTokenController {
);
}
} catch (error) {
logger.error(JSON.stringify({ error }));
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
errorHandler.handle(error);
}
}
);
Expand Down
19 changes: 5 additions & 14 deletions src/controllers/ValidateScoreFileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
Range,
TextDocument,
} from 'vscode';
import { isHumanitecExtensionError } from '../errors/IHumanitecExtensionError';
import { ILoggerService } from '../services/LoggerService';
import { IConfigurationRepository } from '../repos/ConfigurationRepository';
import { ConfigKey } from '../domain/ConfigKey';
import { IErrorHandlerService } from '../services/ErrorHandlerService';

export class ValidateScoreFileController {
private static instance: ValidateScoreFileController;
Expand All @@ -29,7 +28,7 @@ export class ValidateScoreFileController {
context: vscode.ExtensionContext,
validationService: IScoreValidationService,
config: IConfigurationRepository,
logger: ILoggerService
errorHandler: IErrorHandlerService
) {
if (this.instance === undefined) {
this.instance = new ValidateScoreFileController(
Expand Down Expand Up @@ -61,15 +60,7 @@ export class ValidateScoreFileController {
);
}
} catch (error) {
if (isHumanitecExtensionError(error)) {
vscode.window.showErrorMessage(error.message());
logger.error(error.details());
} else {
vscode.window.showErrorMessage(
'Unexpected error occurred. Please contact the extension developer'
);
logger.error(JSON.stringify({ error }));
}
errorHandler.handle(error);
}
});
}
Expand All @@ -95,7 +86,7 @@ export class ValidateScoreFileController {
);
}
} catch (error) {
logger.error(JSON.stringify({ error }));
errorHandler.handle(error);
}
});
});
Expand Down Expand Up @@ -125,7 +116,7 @@ export class ValidateScoreFileController {
);
}
} catch (error) {
logger.error(JSON.stringify({ error }));
errorHandler.handle(error);
}
});
context.subscriptions.push(disposable);
Expand Down
21 changes: 21 additions & 0 deletions src/errors/HumctlError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IHumanitecExtensionError } from './IHumanitecExtensionError';

export class HumctlError implements IHumanitecExtensionError {
constructor(
private readonly command: string,
private readonly output: string,
private readonly exitCode: number
) {}

message(): string {
return 'Command: ' + this.command + ' returned an unexpected error';
}

details(): string {
return JSON.stringify({
command: this.command,
output: this.output,
exitCode: this.exitCode,
});
}
}
12 changes: 0 additions & 12 deletions src/errors/IHumanitecExtensionError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,3 @@ export interface IHumanitecExtensionError {
message(): string;
details(): string;
}

export function isHumanitecExtensionError(
error: unknown
): error is IHumanitecExtensionError {
const isObject = error !== null && typeof error === 'object';
if (isObject) {
const hasMessage = 'message' in error;
const hasDetails = 'details' in error;
return hasMessage && hasDetails;
}
return false;
}
Loading

0 comments on commit 1e3a97b

Please sign in to comment.