prost_validate/
error.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::errors;

/// Represents a validation error for a field.
#[derive(Debug, Clone)]
pub struct Error {
    /// The field associated with the error.
    pub field: String,
    /// The error message.
    pub details: errors::Error,
}

impl Error {
    /// Creates a new `Error` instance.
    #[allow(clippy::needless_pass_by_value)]
    pub fn new<T: ToString>(field: T, details: impl Into<errors::Error>) -> Self {
        Self {
            field: field.to_string(),
            details: details.into(),
        }
    }
}

impl std::fmt::Display for Error {
    /// Formats the error for display.
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "\"{}\": {}", self.field, self.details)
    }
}

#[cfg(feature = "tonic")]
impl From<Error> for tonic_types::FieldViolation {
    /// Converts an `Error` into a `FieldViolation`.
    fn from(value: Error) -> Self {
        Self {
            field: value.field,
            description: value.details.to_string(),
        }
    }
}

#[cfg(feature = "tonic")]
impl From<Error> for tonic_types::ErrorDetails {
    /// Converts an `Error` into `ErrorDetails`.
    fn from(value: Error) -> Self {
        tonic_types::ErrorDetails::with_bad_request(vec![value.into()])
    }
}

#[cfg(feature = "tonic")]
impl From<Error> for tonic::Status {
    /// Converts an `Error` into a `tonic::Status`.
    fn from(value: Error) -> Self {
        let code = match value.details {
            errors::Error::InvalidRules(_) => tonic::Code::Internal,
            _ => tonic::Code::InvalidArgument,
        };
        <tonic::Status as tonic_types::StatusExt>::with_error_details(
            code,
            value.to_string(),
            value.into(),
        )
    }
}

/// Macro to format an error.
///
/// # Arguments
///
/// * `$msg` - The error message.
/// * `$field` - The field associated with the error.
/// * `$arg` - Additional arguments for the error message.
///
/// # Returns
///
/// An `Error` instance.
#[macro_export]
macro_rules! format_err {
    ($msg:literal $(,)?) => {
        ::prost_validate::Error {
            field: "".to_string(),
            details: ::prost_validate::errors::Error::InvalidRules(format!("{}", $msg)),
        }
    };
    ($field:ident, $msg:ident) => {
        ::prost_validate::Error {
            field: format!("{}", $field),
            details: ::prost_validate::errors::Error::InvalidRules(format!("{}", $msg)),
        }
    };
    ($field:expr, $($arg:tt)*) => {
        ::prost_validate::Error {
            field: format!("{}", $field),
            details: ::prost_validate::errors::Error::InvalidRules(format!($($arg)*)),
        }
    };
}

#[cfg(test)]
mod tests {
    use crate::errors::message;
    use crate::Error;

    #[test]
    fn test_format_err() {
        let err = format_err!("field", "something wrong");
        assert_eq!(
            err.to_string(),
            "\"field\": invalid validation rules: something wrong"
        );

        let err = format_err!("field", "something wrong: {}", "value");
        assert_eq!(
            err.to_string(),
            "\"field\": invalid validation rules: something wrong: value"
        );

        let field = "field";
        let value = "value";
        let err = format_err!(field, "something wrong: {value}");
        assert_eq!(
            err.to_string(),
            "\"field\": invalid validation rules: something wrong: value"
        );
    }

    #[cfg(feature = "tonic")]
    #[test]
    fn test_status() {
        use tonic_types::StatusExt;

        let status: tonic::Status = Error::new("field", message::Error::Required).into();
        assert_eq!(status.code(), tonic::Code::InvalidArgument);
        let details = status.get_error_details();
        assert!(details.bad_request().is_some());
        let f = &details.bad_request().unwrap().field_violations;
        assert_eq!(f.len(), 1);
        assert_eq!(f[0].field, "field");
        assert_eq!(f[0].description, "required");
    }
}