Skip to content

Log one line when the CLI rejects an option value - #2544

Merged
Flix6x merged 4 commits into
mainfrom
fix/log-cli-click-errors
Sep 17, 2026
Merged

Flix6x merged 4 commits into
mainfrom
fix/log-cli-click-errors

Conversation

@BelhsanHmida

@BelhsanHmida BelhsanHmida commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Description

  • Click reports option errors on stderr only, so a cron job redirecting stdout records nothing when a command is called wrongly.
  • FlexMeasures' own log file stays silent too: an invocation that fails while parsing its options never reaches any code that logs, and latest_task_runs is filled by a decorator around the command body.
  • Add LoggedClickExceptionCommand in flexmeasures/cli/utils.py: catches click.ClickException while parsing arguments, logs one ERROR line naming the command and Click's message, and re-raises.
  • Click's own stderr output and exit code 2 are unchanged; the line goes through the app logger, into the handlers the host has configured.
  • One line rather than the usage block, so a repeatedly failing cron job does not fill the log.
  • Applied to flexmeasures add report, the command this was found on.
  • Added changelog item in documentation/changelog.rst

Look & Feel

[FLEXMEASURES][2026-09-16 00:38:28,063] ERROR: Click error in `flexmeasures add report`: Invalid value for '--start': Not a valid datetime.
  • Followed by Click's usual usage block on stderr, and exit code 2 as before.
  • Previously the log file was empty.

How to test

  • flexmeasures/cli/tests/test_data_add_fresh_db.py::test_add_report_logs_click_error_for_invalid_start
  • It invokes flexmeasures 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.
  • To confirm it asserts something real: drop cls=LoggedClickExceptionCommand from the add report decorator and re-run, and it fails with an empty caplog.

Further Improvements

  • Widen beyond add report, via the groups' command_class; the commands already passing cls=DeprecatedOptionsCommand override it, so that class would need to inherit the behaviour.
  • Cover click.Abort, which subclasses RuntimeError rather than ClickException and is how most of our own CLI validation fails. Those messages do reach stdout today.
  • Damp repeated failures, as _make_sentry_daily_deduplicator does for Sentry events, if a per-minute command failing continuously turns out to matter.
  • Note in documentation/host/error-monitoring.rst that CLI errors go to stderr and 2>&1 captures them.

Related Items

Closes #2543


Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

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>
@BelhsanHmida BelhsanHmida self-assigned this Sep 15, 2026
BelhsanHmida and others added 2 commits September 16, 2026 00:51
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>
@read-the-docs-community

read-the-docs-community Bot commented Sep 15, 2026

Copy link
Copy Markdown

Documentation build overview

📚 flexmeasures | 🛠️ Build #34602836 | 📁 Comparing 3456e18 against latest (29b2dce)

  🔍 Preview build  

4 files changed
± changelog.html
± genindex.html
± _autosummary/flexmeasures.cli.utils.html
± api/v3_0.html

@Flix6x Flix6x added the CLI label Sep 16, 2026
Comment thread flexmeasures/cli/data_add.py Outdated
@Flix6x Flix6x added this to the 1.1.0 milestone 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>
@BelhsanHmida
BelhsanHmida requested a review from Flix6x September 17, 2026 01:03
@Flix6x
Flix6x merged commit 5c428ac into main Sep 17, 2026
13 checks passed
@Flix6x
Flix6x deleted the fix/log-cli-click-errors branch September 17, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A CLI option error is invisible in the log file a cron job writes

2 participants