Skip to main content

apimock_routing/
error.rs

1//! Errors surfaced by the routing crate.
2//!
3//! # Why routing has its own error type
4//!
5//! Before 5.0, every failure in apimock funnelled into a single
6//! `AppError`. That's natural for a single-crate project but awkward
7//! across a workspace, because `apimock-routing` shouldn't have to know
8//! about TLS failures or listener-address parsing — those are
9//! server-layer concerns. Each of the three crates now defines its own
10//! error variants at the right abstraction level:
11//!
12//! - `apimock-routing::RoutingError`  — rule-set read / parse
13//! - `apimock-config::ConfigError`    — config read / parse, middleware
14//!   compile, path resolution; wraps `RoutingError` when the failure
15//!   came from a rule set
16//! - `apimock-server::ServerError`    — TLS load, listener address
17//!
18//! The façade crate (`apimock`) re-exports all three under one
19//! convenience alias (`AppError`) for existing consumers.
20
21use std::{io, path::PathBuf};
22
23/// Result alias used inside this crate.
24pub type RoutingResult<T> = Result<T, RoutingError>;
25
26/// All fatal errors produced by routing-layer operations.
27#[derive(Debug, thiserror::Error)]
28pub enum RoutingError {
29    /// A rule-set TOML file could not be read.
30    #[error("failed to read rule set file `{path}`: {source}")]
31    RuleSetRead {
32        path: PathBuf,
33        #[source]
34        source: io::Error,
35    },
36
37    /// A rule-set TOML file could not be parsed.
38    #[error("invalid rule set TOML in `{path}`{canonical_display}: {source}", canonical_display = match canonical {
39        Some(p) => format!(" ({})", p.display()),
40        None => String::new(),
41    })]
42    RuleSetParse {
43        path: PathBuf,
44        canonical: Option<PathBuf>,
45        #[source]
46        source: toml::de::Error,
47    },
48}