Skip to content

Commit

Permalink
fix: throw good error for bad json config files
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jun 23, 2023
1 parent d8853bb commit 7becc86
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/config/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,24 @@ export class ConfigFile<
// internally and updated persistently via write().
if (!this.hasRead || force) {
this.logger.info(`Reading config file: ${this.getPath()}`);
const obj = parseJsonMap(await fs.promises.readFile(this.getPath(), 'utf8'));
const obj = parseJsonMap(await fs.promises.readFile(this.getPath(), 'utf8'), this.getPath());
this.setContentsFromObject(obj);
}
// Necessarily set this even when an error happens to avoid infinite re-reading.
// To attempt another read, pass `force=true`.
this.hasRead = true;
return this.getContents();
} catch (err) {
this.hasRead = true;
if ((err as SfError).code === 'ENOENT') {
if (!throwOnNotFound) {
this.setContents();
return this.getContents();
}
}
throw err;
} finally {
// Necessarily set this even when an error happens to avoid infinite re-reading.
// To attempt another read, pass `force=true`.
this.hasRead = true;
throw err;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/stateAggregator/accessors/orgAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export abstract class BaseOrgAccessor<T extends ConfigFile, P extends ConfigCont
this.configs.set(username, config);
return this.get(username, decrypt);
} catch (err) {
if (err instanceof Error && err.name === 'JsonParseError') {
throw err;
}
return null;
}
}
Expand Down

0 comments on commit 7becc86

Please sign in to comment.