relay_server/utils/
api.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::error::Error;
use std::fmt;

use axum::response::IntoResponse;
use serde::{Deserialize, Serialize};

/// Represents an action requested by the Upstream sent in an error message.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub enum RelayErrorAction {
    Stop,
    #[serde(other)]
    #[default]
    None,
}

impl RelayErrorAction {
    fn is_none(&self) -> bool {
        *self == Self::None
    }
}

/// An error response from an api.
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct ApiErrorResponse {
    #[serde(default)]
    detail: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    causes: Vec<String>,
    #[serde(default, skip_serializing_if = "RelayErrorAction::is_none")]
    relay: RelayErrorAction,
}

impl ApiErrorResponse {
    /// Creates an error response with a detail message
    pub fn with_detail<S: AsRef<str>>(s: S) -> ApiErrorResponse {
        ApiErrorResponse {
            detail: Some(s.as_ref().to_string()),
            causes: Vec::new(),
            relay: RelayErrorAction::None,
        }
    }

    pub fn from_error<E: Error + ?Sized>(error: &E) -> Self {
        let detail = Some(error.to_string());

        let mut causes = Vec::new();
        let mut source = error.source();
        while let Some(s) = source {
            causes.push(s.to_string());
            source = s.source();
        }

        Self {
            detail,
            causes,
            relay: RelayErrorAction::None,
        }
    }

    pub fn relay_action(&self) -> RelayErrorAction {
        self.relay
    }
}

impl fmt::Display for ApiErrorResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref detail) = self.detail {
            write!(f, "{detail}")
        } else {
            write!(f, "no error details")
        }
    }
}

impl Error for ApiErrorResponse {}

impl IntoResponse for ApiErrorResponse {
    fn into_response(self) -> axum::response::Response {
        axum::Json(self).into_response()
    }
}