Skip to content

Commit

Permalink
show a descriptive error when scope.json file is not a valid JSON (#4855
Browse files Browse the repository at this point in the history
)

Currently, in this case, it just shows "Unexpected end of JSON input".
  • Loading branch information
davidfirst authored Oct 4, 2021
1 parent 7b71e5b commit 1a66368
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/scope/scope-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,15 @@ export class ScopeJson {
}
}

static loadFromJson(json: string): ScopeJson {
return new ScopeJson(JSON.parse(json));
static loadFromJson(json: string, scopeJsonPath: string): ScopeJson {
let jsonParsed: ScopeJsonProps;
try {
jsonParsed = JSON.parse(json);
} catch (err) {
throw new GeneralError(`unable to parse the scope.json file located at "${scopeJsonPath}".
edit the file to fix the error, or delete it and run "bit init" to recreate it`);
}
return new ScopeJson(jsonParsed);
}

static async loadFromFile(scopeJsonPath: string): Promise<ScopeJson> {
Expand All @@ -151,7 +158,7 @@ export class ScopeJson {
if (err.code === 'ENOENT') throw new ScopeJsonNotFound(scopeJsonPath);
throw err;
}
return ScopeJson.loadFromJson(rawScopeJson.toString());
return ScopeJson.loadFromJson(rawScopeJson.toString(), scopeJsonPath);
}

getPopulatedLicense(): Promise<string | null | undefined> {
Expand Down

0 comments on commit 1a66368

Please sign in to comment.