Design a Rust error enum with context, propagate errors with Result and ?, add retry for transient failures, log with tracing to console and file, and install a panic hook.
Design error handling and logging for the Rust application below. Use Rust's model: recoverable errors are values returned through `Result` and propagated with `?`; panics are for bugs and are reported by a panic hook. There is no runtime catch-all for errors, and syntax errors are caught by the compiler, not at runtime. ## Inputs Application type (CLI, web service, library) and runtime (sync or tokio async): [APP_TYPE] Error sources (file I/O, network/HTTP client, database driver, parsing, configuration): [ERROR_SOURCES] Crates already in use or allowed (for example thiserror, anyhow, tracing, tracing-subscriber, tracing-appender): [CRATES] Logging requirements (console and/or file, format such as JSON, verbosity via environment variable): [LOGGING] Which errors are transient and should be retried: [RETRY_POLICY] ## Instructions 1. Define an error enum (for example `AppError`) with variants such as `Io`, `Network`, `Database` and `Unknown`/`Other`, carrying context (file path, URL, query name) and the source error via `#[source]` or `#[from]`. 2. Implement `std::error::Error` and `Display` (thiserror can derive them). For application code, decide whether `anyhow` with `.context()` is used at the top level. 3. Add a method such as `is_transient()` that classifies errors for [RETRY_POLICY]; retry only those, with capped backoff. 4. Configure `tracing` with `tracing-subscriber`: levels trace, debug, info, warn and error (there is no "fatal" level; treat unrecoverable conditions as error-level events followed by a clean exit). Write to console, file, or both per [LOGGING], with verbosity from an environment filter. 5. Handle errors at the boundary: in `main`, log the full error chain once, map error kinds to exit codes (CLI) or HTTP responses (service), and avoid logging the same error at every layer. 6. Install a panic hook that logs panics through tracing. 7. Keep the design extensible: new error sources add a variant or a `#[from]` conversion without changing call sites. ## Return 1. The error type definition. 2. Logger setup. 3. Top-level error handling in `main` and the panic hook. 4. Example usage: an I/O error when reading a missing file with the path logged; a network error retried and then reported; logging to both console and file with a chosen format. 5. `Cargo.toml` dependency lines with versions marked to confirm. 6. Tests for error classification and Display output. ## Acceptance checks - The code uses `Result` and `?` for propagation; no catch-all error handler is claimed. - No "fatal" log level is used. - Each error is logged once with its full source chain. - Only errors classified as transient are retried. - The code is described as uncompiled unless `cargo build` and `cargo test` were run.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
App: CLI tool that downloads a JSON report over HTTPS, stores it in SQLite and writes a summary file; tokio runtime Error sources: std::fs, reqwest, rusqlite, serde_json Crates: thiserror, anyhow (main only), tracing, tracing-subscriber, tracing-appender Logging: human-readable to stderr, JSON to ./logs/app.log with daily rotation; RUST_LOG controls verbosity Retry: retry HTTP timeouts, connection errors and 5xx up to 3 times; never retry 4xx or database errors
An AppError enum with Io { path, source }, Network { url, source }, Http { url, status }, Database { source }, Parse { source } variants derived with thiserror; is_transient() returning true for timeouts, connection errors and 5xx statuses only; a tracing-subscriber registry with an EnvFilter from RUST_LOG, a stderr layer and a JSON layer writing through a tracing-appender daily rolling file; main returning anyhow::Result and logging the error chain once; a panic hook that emits an error event; examples for a missing file and a retried 503; and dependency lines marked to confirm versions. Illustrative expectation, not a recorded model run; no Rust toolchain was available for a local compile check (see report).Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts: