Skip to content

[config] Config::load returns anyhow::Result — validation errors are opaque strings; typed ConfigError needed for half-wired and env-missing cases #255

@obchain

Description

@obchain

PR: #51 (feat/23-testnet-config)
File: crates/charon-core/src/config.rs
Refs #51

Problem

Config::load() returns anyhow::Result<Self>. Every error — file-not-found, unset env var, TOML parse failure, and now the new validation failure (half-wired config) — surfaces as an untyped anyhow chain string.

This PR extends Config::load() to call a new validate() method. Validation errors expressed as anyhow strings cannot be matched by the CLI to give actionable recovery hints. An operator running the testnet profile for the first time gets:

Error: env var `CHARON_BNB_TESTNET_WS_URL` is not set

with no guidance on where to set it. A typed error would allow the CLI to add context ("Check .env or export the variable before starting").

Recurring thiserror convention: PRs #28, #39, #41, #44, #46 all flagged the same pattern on public lib APIs.

Fix

Define:

#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ConfigError {
    #[error("failed to read config file {path}: {source}")]
    FileRead { path: PathBuf, #[source] source: std::io::Error },
    #[error("env var `{var}` is not set (referenced in config at {path})")]
    EnvVarMissing { var: String, path: PathBuf },
    #[error("unterminated `${{" placeholder in config {path}")]
    UnterminatedPlaceholder { path: PathBuf },
    #[error("failed to parse TOML at {path}: {source}")]
    TomlParse { path: PathBuf, #[source] source: toml::de::Error },
    #[error("half-wired config: chain '{chain}' has {present} but not {missing}")]
    HalfWired { chain: String, present: &'static str, missing: &'static str },
}

Change Config::load to Result<Self, ConfigError>. Add thiserror to charon-core/Cargo.toml and [workspace.dependencies].

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestlayer:rustRust crates (core / scanner / protocols / executor / cli)priority:p2-polishNice-to-have / polishstatus:readyScoped and ready to pick up

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions