-
Notifications
You must be signed in to change notification settings - Fork 52
fix(tracker-client): improve error message when JSON config is not well-formatted (#1042) #1764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
josecelano
merged 9 commits into
torrust:develop
from
josecelano:1042-tracker-checker-improve-error-message-json-config
May 12, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a0476ea
docs(issues): update #1042 spec with I/O contract alignment and testi…
josecelano 08b89be
fix(tracker-client): surface JSON parse detail in checker config erro…
josecelano 2c9f5f8
docs(issues): update #1042 frontmatter — status and timestamps
josecelano b28c54c
chore(dev): add dep
josecelano 8114cd4
docs(issues): add manual verification section to #1042
josecelano e2ea3a9
chore(tracker-client): fix linter warnings in #1042 implementation
josecelano dbba1ba
docs(issues): link PR #1764 to issue spec #1042
josecelano 8bb52e4
fix(tracker-client): address copilot review on #1764
josecelano 36ed8ef
test(tracker-client): make tracker_checker binary lookup nextest-safe
josecelano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
186 changes: 186 additions & 0 deletions
186
console/tracker-client/src/console/clients/checker/error.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| //! Application-level errors for the tracker checker binary. | ||
| //! | ||
| //! This module separates two concerns: | ||
| //! - **Delivery mechanism**: how the configuration was provided (env var, file path, …) | ||
| //! - **Error presentation**: what structured JSON the binary emits on stderr | ||
| //! | ||
| //! `ConfigSource` captures the delivery mechanism so that error messages can | ||
| //! reference it without coupling the parsing layer to delivery specifics. | ||
| //! | ||
| //! The JSON envelope emitted to stderr follows the Tracker CLI I/O Contract: | ||
| //! | ||
| //! ```json | ||
| //! { "error": { "kind": "...", "source": "...", "message": "..." } } | ||
| //! ``` | ||
| use std::fmt; | ||
| use std::path::PathBuf; | ||
|
|
||
| /// Where the configuration content was delivered from. | ||
| #[derive(Debug, Clone)] | ||
| pub enum ConfigSource { | ||
| /// Configuration delivered via an environment variable (stores the variable name). | ||
| EnvVar(&'static str), | ||
| /// Configuration delivered via a file (stores the file path). | ||
| File(PathBuf), | ||
| } | ||
|
|
||
| impl fmt::Display for ConfigSource { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| ConfigSource::EnvVar(name) => write!(f, "{name}"), | ||
| ConfigSource::File(path) => write!(f, "{}", path.display()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Top-level application errors for the tracker checker. | ||
| #[derive(Debug)] | ||
| pub enum AppError { | ||
| /// The provided configuration was invalid (bad JSON, invalid URLs, etc.). | ||
| InvalidConfig { | ||
| /// How the configuration was delivered (env var or file path). | ||
| source: ConfigSource, | ||
| /// Human-readable detail from the underlying parse error. | ||
| message: String, | ||
| }, | ||
| /// An unexpected runtime failure occurred after configuration was accepted. | ||
| Runtime(String), | ||
| } | ||
|
|
||
| impl AppError { | ||
| /// Serializes the error to the contract JSON envelope and returns the | ||
| /// appropriate process exit code. | ||
| /// | ||
| /// Exit codes: | ||
| /// - `2` — configuration error | ||
| /// - `1` — generic runtime failure | ||
| #[must_use] | ||
| pub fn to_stderr_json_and_exit_code(&self) -> (String, i32) { | ||
| match self { | ||
| AppError::InvalidConfig { source, message } => { | ||
| let json = serde_json::json!({ | ||
| "error": { | ||
| "kind": "invalid_configuration", | ||
| "source": source.to_string(), | ||
| "message": message, | ||
| } | ||
| }) | ||
| .to_string(); | ||
| (json, 2) | ||
| } | ||
| AppError::Runtime(message) => { | ||
| let json = serde_json::json!({ | ||
| "error": { | ||
| "kind": "runtime_failure", | ||
| "source": "runtime", | ||
| "message": message, | ||
| } | ||
| }) | ||
| .to_string(); | ||
| (json, 1) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for AppError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| AppError::InvalidConfig { source, message } => { | ||
| write!(f, "invalid configuration from {source}: {message}") | ||
| } | ||
| AppError::Runtime(msg) => write!(f, "runtime failure: {msg}"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn config_source_env_var_displays_as_variable_name() { | ||
| let source = ConfigSource::EnvVar("TORRUST_CHECKER_CONFIG"); | ||
| assert_eq!(source.to_string(), "TORRUST_CHECKER_CONFIG"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn config_source_file_displays_as_path() { | ||
| let source = ConfigSource::File(PathBuf::from("/etc/tracker/config.json")); | ||
| assert_eq!(source.to_string(), "/etc/tracker/config.json"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_config_error_produces_exit_code_2() { | ||
| let error = AppError::InvalidConfig { | ||
| source: ConfigSource::EnvVar("TORRUST_CHECKER_CONFIG"), | ||
| message: "JSON parse error: trailing comma at line 7 column 5".to_string(), | ||
| }; | ||
| let (_, exit_code) = error.to_stderr_json_and_exit_code(); | ||
| assert_eq!(exit_code, 2); | ||
| } | ||
|
|
||
| #[test] | ||
| fn runtime_error_produces_exit_code_1() { | ||
| let error = AppError::Runtime("failed to bind socket".to_string()); | ||
| let (_, exit_code) = error.to_stderr_json_and_exit_code(); | ||
| assert_eq!(exit_code, 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_config_error_json_contains_expected_fields() { | ||
| let error = AppError::InvalidConfig { | ||
| source: ConfigSource::EnvVar("TORRUST_CHECKER_CONFIG"), | ||
| message: "JSON parse error: trailing comma at line 7 column 5".to_string(), | ||
| }; | ||
| let (json, _) = error.to_stderr_json_and_exit_code(); | ||
| let parsed: serde_json::Value = serde_json::from_str(&json).expect("Error JSON should be valid JSON"); | ||
|
|
||
| assert_eq!(parsed["error"]["kind"], "invalid_configuration"); | ||
| assert_eq!(parsed["error"]["source"], "TORRUST_CHECKER_CONFIG"); | ||
| assert_eq!( | ||
| parsed["error"]["message"], | ||
| "JSON parse error: trailing comma at line 7 column 5" | ||
| ); | ||
| } | ||
|
josecelano marked this conversation as resolved.
|
||
|
|
||
| #[test] | ||
| fn runtime_error_json_contains_expected_fields() { | ||
| let error = AppError::Runtime("failed to bind socket".to_string()); | ||
| let (json, _) = error.to_stderr_json_and_exit_code(); | ||
| let parsed: serde_json::Value = serde_json::from_str(&json).expect("Error JSON should be valid JSON"); | ||
|
|
||
| assert_eq!(parsed["error"]["kind"], "runtime_failure"); | ||
| assert_eq!(parsed["error"]["source"], "runtime"); | ||
| assert_eq!(parsed["error"]["message"], "failed to bind socket"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_config_error_from_file_includes_path_in_json() { | ||
| let error = AppError::InvalidConfig { | ||
| source: ConfigSource::File(PathBuf::from("/etc/tracker/config.json")), | ||
| message: "JSON parse error: trailing comma at line 3 column 1".to_string(), | ||
| }; | ||
| let (json, _) = error.to_stderr_json_and_exit_code(); | ||
| let parsed: serde_json::Value = serde_json::from_str(&json).expect("Error JSON should be valid JSON"); | ||
|
|
||
| assert_eq!(parsed["error"]["source"], "/etc/tracker/config.json"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_config_error_json_escapes_special_characters() { | ||
| let source_path = r"C:\tracker\config\broken.json"; | ||
| let message = "JSON parse error: unexpected '\"' on line 2\nCheck C:\\temp\\config.json"; | ||
|
|
||
| let error = AppError::InvalidConfig { | ||
| source: ConfigSource::File(PathBuf::from(source_path)), | ||
| message: message.to_string(), | ||
| }; | ||
| let (json, _) = error.to_stderr_json_and_exit_code(); | ||
| let parsed: serde_json::Value = serde_json::from_str(&json).expect("Error JSON should be valid JSON"); | ||
|
|
||
| assert_eq!(parsed["error"]["kind"], "invalid_configuration"); | ||
| assert_eq!(parsed["error"]["source"], source_path); | ||
| assert_eq!(parsed["error"]["message"], message); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.