Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 14 additions & 14 deletions extensions/ql-vscode/src/common/value-result.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
/**
* Represents a result that can be either a value or some errors.
*/
export class ValueResult<TValue> {
export class ValueResult<TValue, TError> {
private constructor(
private readonly errorMsgs: string[],
private readonly errs: TError[],
private readonly val?: TValue,
) {}

public static ok<TValue>(value: TValue): ValueResult<TValue> {
public static ok<TValue, TError>(value: TValue): ValueResult<TValue, TError> {
if (value === undefined) {
throw new Error("Value must be set for successful result");
}

return new ValueResult([], value);
}

public static fail<TValue>(errorMsgs: string[]): ValueResult<TValue> {
if (errorMsgs.length === 0) {
throw new Error(
"At least one error message must be set for a failed result",
);
public static fail<TValue, TError>(
errors: TError[],
): ValueResult<TValue, TError> {
if (errors.length === 0) {
throw new Error("At least one error must be set for a failed result");
}

return new ValueResult<TValue>(errorMsgs, undefined);
return new ValueResult<TValue, TError>(errors, undefined);
}

public get isOk(): boolean {
return this.errorMsgs.length === 0;
return this.errs.length === 0;
}

public get isFailure(): boolean {
return this.errorMsgs.length > 0;
return this.errs.length > 0;
}

public get errors(): string[] {
if (!this.errorMsgs) {
public get errors(): TError[] {
if (!this.errs) {
throw new Error("Cannot get error for successful result");
}

return this.errorMsgs;
return this.errs;
}

public get value(): TValue {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class DbConfigStore extends DisposableObject {
this.configWatcher?.unwatch(this.configPath);
}

public getConfig(): ValueResult<DbConfig> {
public getConfig(): ValueResult<DbConfig, string> {
if (this.config) {
// Clone the config so that it's not modified outside of this class.
return ValueResult.ok(cloneDbConfig(this.config));
Expand Down
2 changes: 1 addition & 1 deletion extensions/ql-vscode/src/databases/db-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DbManager {
return getSelectedDbItem(dbItems.value);
}

public getDbItems(): ValueResult<DbItem[]> {
public getDbItems(): ValueResult<DbItem[], string> {
const configResult = this.dbConfigStore.getConfig();
if (configResult.isFailure) {
return ValueResult.fail(configResult.errors);
Expand Down