Slightly increase the tutorial's code snippets consistency#7319
Slightly increase the tutorial's code snippets consistency#7319Fedeton wants to merge 2 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request updates several tutorial markdown files to correct exception messages, throw ArgumentException instead of returning error strings, add a newline to search results, and print caught exceptions. Feedback recommends using simpler string interpolation (
| 'Input ${input} is not a known command.', | ||
| name |
There was a problem hiding this comment.
According to the Dart style guide (Effective Dart), you should prefer interpolating pure identifiers directly without curly braces (i.e., $input instead of ${input}). Additionally, adding a trailing comma after name is recommended for consistent formatting in multi-line argument lists.
| 'Input ${input} is not a known command.', | |
| name | |
| 'Input $input is not a known command.', | |
| name, |
References
- Avoid curly braces in interpolation when just an identifier. (link)
Tutorial code snippets a little more consistent
The changes in the commit slightly increase the consistency of the code snippets in the tutorial.
Polish your CLI app
When running
cli.dart help --command <input>an exception is thrown if<input>is not a valid command as shown in the following piece of code where<input>is contained in the variable namedinput:Here,
args.commandArgis the positional argument's value for thehelpcommand, but the user wants to view the details of the command specified as value to the option--command. So, shouldn't theinputvariable be used instead ofargs.commandArgwhen creating the exception message?Furthermore, the name of the running command is also passed to the constructor of the
ArgumentExceptionclass.Fetch data from the internet
In this chapter are defined the functions
getRandomArticleSummary(),getArticleSummaryByTitle(),search()andgetArticleByTitle(). Each function makes the proper API request and then throws anHttpExceptionif the response's status code is not 200. This commit uniforms the exception message by using the same message prefix for all functions and by specifying the exact name of the function that is throwing the exception.Add logging for debugging and monitoring
The first change for this chapter is within the
main()function in which theonErrorcallback is passed to theCommandRunnerconstructor. If the error received by this callback is anExceptioninstance, then it is not printed to the console anymore, it's instead just logged. This causes anArgumentExceptionerror to not be shown as console output and the user must look into the log file. With this commit, the exception is printed again after being logged.The second change is made in the
run()method of theSearchCommandclass. Now, the arguments validation will throw anArgumentExceptioninstead of just returning a string. This behavior is consistent with that of theHelpCommandclass.The last change is also in the
run()method ofSearchCommandclass and it adds a new line character between the string'Search results:'and the first result element.