Skip to content

Conversation

@andrefarzat
Copy link
Contributor

Issue:
When running npx sb init in an angular project, one json file was wrong (with a trailing comma) and it took me too much time to figure out which json file was wrong.
Screen Shot 2021-01-26 at 10 18 26

What I did

Added try/catch wrapping the JSON.parse to improve error message and the user be aware which json file is wrong

How to test

My test case is having a tsconfig.app.json with a wrong json in the file. In an angular project, the command npx sb init reads about 3 json files ( among angular.json, tsconfig.json and tsconfig.app.json )

Copy link
Contributor

@phated phated left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good idea! I just don't want to lose the stack trace, instead we should add additional information somehow. 🤔

try {
return JSON.parse(jsonContent);
} catch(e) {
throw new Error('Invalid json on file: ' + filePath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should log the filename and still throw the original error? I would hate to lose the stack trace for debugging

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as you suggested. Just added chalk.red on it:

try {
    return JSON.parse(jsonContent);
  } catch(e) {
    console.error(chalk.red('Invalid json on file: ' + filePath));
    throw e;
}

@andrefarzat andrefarzat requested a review from phated January 29, 2021 14:33
@shilman shilman changed the title Adding try/catch on readFileAsJson to improve error message Core: Add try/catch on readFileAsJson to improve error message Feb 2, 2021
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay. Thanks @andrefarzat !!!

@shilman shilman changed the title Core: Add try/catch on readFileAsJson to improve error message CLI: Add try/catch on readFileAsJson to improve error message Feb 2, 2021
@shilman shilman merged commit 12424cb into storybookjs:next Feb 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants