Skip to content

fix(tracker-client): improve error message when JSON config is not well-formatted (#1042)#1764

Merged
josecelano merged 9 commits into
torrust:developfrom
josecelano:1042-tracker-checker-improve-error-message-json-config
May 12, 2026
Merged

fix(tracker-client): improve error message when JSON config is not well-formatted (#1042)#1764
josecelano merged 9 commits into
torrust:developfrom
josecelano:1042-tracker-checker-improve-error-message-json-config

Conversation

@josecelano
Copy link
Copy Markdown
Member

Summary

Fixes #1042 — Tracker Checker (HTTP): Improve Error Message When JSON Config Is Not Well-Formatted

When the Tracker Checker received a malformed JSON configuration (e.g. a trailing comma), it
panicked with a generic invalid config format message. The detailed parse error was buried in
the "Caused by" chain, invisible without RUST_BACKTRACE=1.

Changes

  • console/tracker-client/src/console/clients/checker/error.rs (new): ConfigSource enum
    (env var or file path) and AppError enum (InvalidConfig / Runtime) with JSON serialization
    to the Tracker CLI I/O Contract schema.
  • console/tracker-client/src/console/clients/checker/app.rs: Refactored setup_config and
    load_config_from_file to return AppError instead of anyhow. Parse errors now propagate
    with source context instead of a generic wrapper.
  • console/tracker-client/src/bin/tracker_checker.rs: Replaced .expect() panic with
    structured error handling — JSON error written to stderr, exits with code 2 for config errors.
  • console/tracker-client/src/console/clients/checker/config.rs: Added 5 new unit tests for
    parse error propagation.
  • console/tracker-client/tests/tracker_checker.rs (new): 9 end-to-end integration tests
    covering env var and file config sources.

Error Output (before vs. after)

Before:

thread 'main' panicked at console/tracker-client/src/bin/tracker_checker.rs:6:22:
Some checks fail: invalid config format

Caused by:
    JSON parse error: trailing comma at line 7 column 5

After:

{"error":{"kind":"invalid_configuration","source":"TORRUST_CHECKER_CONFIG","message":"JSON parse error: trailing comma at line 7 column 5"}}

Exit code: 2

Testing

  • 35 unit tests pass (including 12 new)
  • 9 integration tests pass (new)
  • All linters pass (linter all exits 0)
  • Manual verification with tracker-demo URLs documented in the issue spec

Issue Spec

docs/issues/open/1042-tracker-checker-http-improve-error-message-json-config.md

…rs (torrust#1042)

- Added `error.rs` with `ConfigSource` and `AppError` types that decouple delivery mechanism from error presentation
- Replaced generic `.context("invalid config format")` with source-aware `AppError::InvalidConfig`
- Binary no longer panics on config errors; prints JSON envelope to stderr and exits with code 2 (config) or 1 (runtime)
- Follows Tracker CLI I/O Contract: `{"error":{"kind":"...","source":"...","message":"..."}}`
- Added 12 unit tests and 9 integration tests; all 44 tests pass
Reflect that implementation is now in-progress following rebase completion:
- Update status: planned → in-progress
- Correct branch name to match actual branch
- Update last-updated timestamp to reflect rebase completion
- Replace skill-link references with actual I/O contract ADR and documentation
Document 6 manual test scenarios covering:
- Valid configuration with tracker demo URLs
- JSON parse error from environment variable (trailing comma)
- JSON parse error from environment variable (missing bracket)
- JSON parse error from configuration file (shows file path in source)
- Missing configuration (no config provided)
- Configuration validation error (invalid URL)

All tests pass and verify exit codes (0 for success, 2 for config errors) and
error output format. Error envelopes are compliant with Tracker CLI I/O Contract
schema and are correctly written to stderr.
@josecelano josecelano self-assigned this May 12, 2026
@josecelano josecelano requested a review from Copilot May 12, 2026 13:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the tracker_checker CLI to surface configuration/JSON parsing failures as contract-compliant structured JSON errors (stderr) with deterministic exit codes, instead of panicking with a generic message.

Changes:

  • Introduces AppError/ConfigSource for checker-specific error classification and formatting.
  • Refactors config loading/parsing to return structured config errors and maps runtime failures to exit code 1.
  • Adds unit + integration tests for malformed/missing configuration and exit code semantics.

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
docs/issues/open/1042-tracker-checker-http-improve-error-message-json-config.md Updates the issue spec to reference the CLI I/O contract and acceptance evidence.
console/tracker-client/tests/tracker_checker.rs Adds E2E tests asserting stderr JSON envelope + exit code 2 for config errors.
console/tracker-client/src/console/clients/checker/mod.rs Exposes the new error module.
console/tracker-client/src/console/clients/checker/error.rs Adds structured AppError + config source tracking and JSON error envelope generation.
console/tracker-client/src/console/clients/checker/config.rs Adds unit tests ensuring parse/validation error details are preserved.
console/tracker-client/src/console/clients/checker/app.rs Replaces anyhow config errors with AppError and propagates parse details.
console/tracker-client/src/bin/tracker_checker.rs Replaces panic-on-error with JSON-to-stderr + exit code handling.
console/tracker-client/Cargo.toml Adds tempfile dev-dependency for integration tests.
Cargo.lock Locks tempfile dependency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread console/tracker-client/src/console/clients/checker/error.rs
Comment thread docs/issues/open/1042-tracker-checker-http-improve-error-message-json-config.md Outdated
Comment thread console/tracker-client/src/console/clients/checker/error.rs
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

❌ Patch coverage is 94.93671% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.05%. Comparing base (5ca4de6) to head (36ed8ef).

Files with missing lines Patch % Lines
...racker-client/src/console/clients/checker/error.rs 93.54% 6 Missing ⚠️
console/tracker-client/src/bin/tracker_checker.rs 80.00% 0 Missing and 1 partial ⚠️
.../tracker-client/src/console/clients/checker/app.rs 95.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1764      +/-   ##
===========================================
- Coverage    79.44%   79.05%   -0.40%     
===========================================
  Files          369      370       +1     
  Lines        27328    27682     +354     
  Branches     27328    27682     +354     
===========================================
+ Hits         21710    21883     +173     
- Misses        5337     5517     +180     
- Partials       281      282       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@josecelano
Copy link
Copy Markdown
Member Author

ACK 36ed8ef

@josecelano josecelano merged commit e89441a into torrust:develop May 12, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracker Checker (HTTP): Improve error message when the JSON config is not well-formatted

2 participants