Log one line when the CLI rejects an option value - #2544
Merged
Merged
Conversation
A reporting cron job stopped producing data, and the log file the wrapper script writes held no trace of why. The command had a stray --start, which swallowed the option that followed it as its value, and Click rejected that as a datetime. Click reports such errors on stderr alone, while the script redirected only stdout, so nothing was recorded. FlexMeasures configures a rotating file handler of its own, but an invocation that fails while parsing its options never reaches any code that logs, so that file stays silent too. The same goes for latest_task_runs, which is filled by a decorator around the command body. Add LoggedClickExceptionCommand, which logs one ERROR line naming the command and the message Click would print, and re-raises so that Click's own output and its exit code are unchanged. One line, rather than the usage block, keeps a repeatedly failing cron job from filling the log. Applied to `flexmeasures add report` for now, which is the command this was found on. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>
Documentation build overview
4 files changed± changelog.html± genindex.html± _autosummary/flexmeasures.cli.utils.html± api/v3_0.html |
Flix6x
reviewed
Sep 16, 2026
`add forecasts`, `add schedule` and `jobs run-automations` are run from cron just as `add report` is, and a per-command `cls` would leave each new command to remember to opt in. Put the logging on the group instead. A command's error passes through its group on the way out, so one class on each of the seven groups covers every command in them, including the ones that pass a `cls` of their own, and it picks up the group's own errors too, such as an unknown subcommand. The context Click attaches to the error names the command that failed, which is what the line reports, so a command's error is still reported against the command rather than against its group. Each exception is logged by the first handler to see it and skipped by the rest, so a failure is logged once. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Flix6x
approved these changes
Sep 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
latest_task_runsis filled by a decorator around the command body.LoggedClickExceptionCommandinflexmeasures/cli/utils.py: catchesclick.ClickExceptionwhile parsing arguments, logs oneERRORline naming the command and Click's message, and re-raises.flexmeasures add report, the command this was found on.documentation/changelog.rstLook & Feel
[FLEXMEASURES][2026-09-16 00:38:28,063] ERROR: Click error in `flexmeasures add report`: Invalid value for '--start': Not a valid datetime.How to test
flexmeasures/cli/tests/test_data_add_fresh_db.py::test_add_report_logs_click_error_for_invalid_startflexmeasures add report --start ""through the app CLI runner and asserts Click still exits 2 with its error on stderr, the single line is in the log, and the usage block is not.cls=LoggedClickExceptionCommandfrom theadd reportdecorator and re-run, and it fails with an emptycaplog.Further Improvements
add report, via the groups'command_class; the commands already passingcls=DeprecatedOptionsCommandoverride it, so that class would need to inherit the behaviour.click.Abort, which subclassesRuntimeErrorrather thanClickExceptionand is how most of our own CLI validation fails. Those messages do reach stdout today._make_sentry_daily_deduplicatordoes for Sentry events, if a per-minute command failing continuously turns out to matter.documentation/host/error-monitoring.rstthat CLI errors go to stderr and2>&1captures them.Related Items
Closes #2543
Sign-off