Skip to main content

praxis_core/
errors.rs

1// SPDX-License-Identifier: LGPL-3.0-only
2// Copyright (c) 2024 Shane Utt
3
4//! Shared error types for the Praxis workspace.
5//!
6//! [`ProxyError`] is the primary error type, re-exported from `praxis_core`.
7
8use thiserror::Error;
9
10// -----------------------------------------------------------------------------
11// Errors
12// -----------------------------------------------------------------------------
13
14/// Errors that can occur during proxy operation.
15///
16/// ```
17/// use praxis_core::ProxyError;
18///
19/// let e = ProxyError::Config("bad yaml".into());
20/// assert_eq!(e.to_string(), "config: bad yaml");
21///
22/// let e = ProxyError::NoRoute("GET /missing".into());
23/// assert_eq!(e.to_string(), "no route for GET /missing");
24///
25/// let e = ProxyError::NoUpstream("backend".into());
26/// assert_eq!(e.to_string(), "no upstream in cluster 'backend'");
27/// ```
28#[derive(Debug, Error)]
29pub enum ProxyError {
30    /// Configuration loading or validation error.
31    #[error("config: {0}")]
32    Config(String),
33
34    /// No route matched the incoming request.
35    #[error("no route for {0}")]
36    NoRoute(String),
37
38    /// No upstream available in the given cluster.
39    #[error("no upstream in cluster '{0}'")]
40    NoUpstream(String),
41}
42
43// -----------------------------------------------------------------------------
44// Tests
45// -----------------------------------------------------------------------------
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn error_display() {
53        let e = ProxyError::Config("bad yaml".into());
54        assert_eq!(e.to_string(), "config: bad yaml", "Config error display mismatch");
55
56        let e = ProxyError::NoRoute("GET /missing".into());
57        assert_eq!(
58            e.to_string(),
59            "no route for GET /missing",
60            "NoRoute error display mismatch"
61        );
62
63        let e = ProxyError::NoUpstream("backend".into());
64        assert_eq!(
65            e.to_string(),
66            "no upstream in cluster 'backend'",
67            "NoUpstream error display mismatch"
68        );
69    }
70}