Skip to content

Commit e18a330

Browse files
authored
Update ValueResult to have generic error type (#1861)
1 parent 3b197a2 commit e18a330

3 files changed

Lines changed: 16 additions & 16 deletions

File tree

extensions/ql-vscode/src/common/value-result.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
/**
22
* Represents a result that can be either a value or some errors.
33
*/
4-
export class ValueResult<TValue> {
4+
export class ValueResult<TValue, TError> {
55
private constructor(
6-
private readonly errorMsgs: string[],
6+
private readonly errs: TError[],
77
private readonly val?: TValue,
88
) {}
99

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

1515
return new ValueResult([], value);
1616
}
1717

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

25-
return new ValueResult<TValue>(errorMsgs, undefined);
25+
return new ValueResult<TValue, TError>(errors, undefined);
2626
}
2727

2828
public get isOk(): boolean {
29-
return this.errorMsgs.length === 0;
29+
return this.errs.length === 0;
3030
}
3131

3232
public get isFailure(): boolean {
33-
return this.errorMsgs.length > 0;
33+
return this.errs.length > 0;
3434
}
3535

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

41-
return this.errorMsgs;
41+
return this.errs;
4242
}
4343

4444
public get value(): TValue {

extensions/ql-vscode/src/databases/config/db-config-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class DbConfigStore extends DisposableObject {
4848
this.configWatcher?.unwatch(this.configPath);
4949
}
5050

51-
public getConfig(): ValueResult<DbConfig> {
51+
public getConfig(): ValueResult<DbConfig, string> {
5252
if (this.config) {
5353
// Clone the config so that it's not modified outside of this class.
5454
return ValueResult.ok(cloneDbConfig(this.config));

extensions/ql-vscode/src/databases/db-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class DbManager {
3333
return getSelectedDbItem(dbItems.value);
3434
}
3535

36-
public getDbItems(): ValueResult<DbItem[]> {
36+
public getDbItems(): ValueResult<DbItem[], string> {
3737
const configResult = this.dbConfigStore.getConfig();
3838
if (configResult.isFailure) {
3939
return ValueResult.fail(configResult.errors);

0 commit comments

Comments
 (0)