@@ -4,12 +4,14 @@ import { cloneDbConfig, DbConfig } from './db-config';
44import * as chokidar from 'chokidar' ;
55import { DisposableObject } from '../pure/disposable-object' ;
66import { DbConfigValidator } from './db-config-validator' ;
7+ import { ValueResult } from '../common/value-result' ;
78
89export class DbConfigStore extends DisposableObject {
910 private readonly configPath : string ;
1011 private readonly configValidator : DbConfigValidator ;
1112
12- private config : DbConfig ;
13+ private config : DbConfig | undefined ;
14+ private configErrors : string [ ] ;
1315 private configWatcher : chokidar . FSWatcher | undefined ;
1416
1517 public constructor (
@@ -20,6 +22,7 @@ export class DbConfigStore extends DisposableObject {
2022 this . configPath = path . join ( workspaceStoragePath , 'workspace-databases.json' ) ;
2123
2224 this . config = this . createEmptyConfig ( ) ;
25+ this . configErrors = [ ] ;
2326 this . configWatcher = undefined ;
2427 this . configValidator = new DbConfigValidator ( extensionPath ) ;
2528 }
@@ -33,19 +36,19 @@ export class DbConfigStore extends DisposableObject {
3336 this . configWatcher ?. unwatch ( this . configPath ) ;
3437 }
3538
36- public getConfig ( ) : DbConfig {
37- // Clone the config so that it's not modified outside of this class.
38- return cloneDbConfig ( this . config ) ;
39+ public getConfig ( ) : ValueResult < DbConfig > {
40+ if ( this . config ) {
41+ // Clone the config so that it's not modified outside of this class.
42+ return ValueResult . ok ( cloneDbConfig ( this . config ) ) ;
43+ } else {
44+ return ValueResult . fail ( this . configErrors ) ;
45+ }
3946 }
4047
4148 public getConfigPath ( ) : string {
4249 return this . configPath ;
4350 }
4451
45- public validateConfig ( ) : string [ ] {
46- return this . configValidator . validate ( this . config ) ;
47- }
48-
4952 private async loadConfig ( ) : Promise < void > {
5053 if ( ! await fs . pathExists ( this . configPath ) ) {
5154 await fs . writeJSON ( this . configPath , this . createEmptyConfig ( ) , { spaces : 2 } ) ;
@@ -55,11 +58,33 @@ export class DbConfigStore extends DisposableObject {
5558 }
5659
5760 private async readConfig ( ) : Promise < void > {
58- this . config = await fs . readJSON ( this . configPath ) ;
61+ let newConfig : DbConfig | undefined = undefined ;
62+ try {
63+ newConfig = await fs . readJSON ( this . configPath ) ;
64+ } catch ( e ) {
65+ this . configErrors = [ `Failed to read config file: ${ this . configPath } ` ] ;
66+ }
67+
68+ if ( newConfig ) {
69+ this . configErrors = this . configValidator . validate ( newConfig ) ;
70+ }
71+
72+ this . config = this . configErrors . length === 0 ? newConfig : undefined ;
5973 }
6074
6175 private readConfigSync ( ) : void {
62- this . config = fs . readJSONSync ( this . configPath ) ;
76+ let newConfig : DbConfig | undefined = undefined ;
77+ try {
78+ newConfig = fs . readJSONSync ( this . configPath ) ;
79+ } catch ( e ) {
80+ this . configErrors = [ `Failed to read config file: ${ this . configPath } ` ] ;
81+ }
82+
83+ if ( newConfig ) {
84+ this . configErrors = this . configValidator . validate ( newConfig ) ;
85+ }
86+
87+ this . config = this . configErrors . length === 0 ? newConfig : undefined ;
6388 }
6489
6590 private watchConfig ( ) : void {
0 commit comments