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
15//!                                      `RoutingError` when the failure
16//!                                      came from a rule set
17//! - `apimock-server::ServerError`    — TLS load, listener address
18//!
19//! The façade crate (`apimock`) re-exports all three under one
20//! convenience alias (`AppError`) for existing consumers.
21
22use std::{io, path::PathBuf};
23
24/// Result alias used inside this crate.
25pub type RoutingResult<T> = Result<T, RoutingError>;
26
27/// All fatal errors produced by routing-layer operations.
28#[derive(Debug, thiserror::Error)]
29pub enum RoutingError {
30    /// A rule-set TOML file could not be read.
31    #[error("failed to read rule set file `{path}`: {source}")]
32    RuleSetRead {
33        path: PathBuf,
34        #[source]
35        source: io::Error,
36    },
37
38    /// A rule-set TOML file could not be parsed.
39    #[error("invalid rule set TOML in `{path}`{canonical_display}: {source}", canonical_display = match canonical {
40        Some(p) => format!(" ({})", p.display()),
41        None => String::new(),
42    })]
43    RuleSetParse {
44        path: PathBuf,
45        canonical: Option<PathBuf>,
46        #[source]
47        source: toml::de::Error,
48    },
49}