ichen_openprotocol/
error.rs1use derive_more::*;
2use std::borrow::Cow;
3
4#[derive(Debug, Display)]
7pub enum OpenProtocolError<'a> {
8 #[display(fmt = "field {} cannot be empty or all whitespace", _0)]
11 EmptyField(&'a str),
12 #[display(fmt = "value [{}] is invalid for the field {} - {}", value, field, description)]
15 InvalidField { field: &'a str, value: Cow<'a, str>, description: Cow<'a, str> },
16 #[display(fmt = "value of field {} is not the same as the matching field in the state", _0)]
22 InconsistentState(&'a str),
23 #[display(
29 fmt = "value of field {} is not the same as the matching field in the Controller",
30 _0
31 )]
32 InconsistentField(&'a str),
33 #[display(fmt = "{}", _0)]
36 ConstraintViolated(Cow<'a, str>),
37 #[display(fmt = "[{:?}] {}", "_0.classify()", _0)]
40 JsonError(serde_json::Error),
41 #[display(fmt = "{}", _0)]
44 SystemError(Cow<'a, str>),
45}
46
47impl std::error::Error for OpenProtocolError<'_> {
48 fn description(&self) -> &str {
49 match self {
50 Self::JsonError(err) => err.description(),
52 Self::InvalidField { description, .. } => {
55 if description.is_empty() {
56 "invalid field value"
57 } else {
58 description
59 }
60 }
61 Self::ConstraintViolated(err) => err,
64 Self::SystemError(err) => err,
67 Self::InconsistentField(_) => {
70 "value of field is not the same as matching field in the Controller"
71 }
72 Self::InconsistentState(_) => {
75 "value of field is not the same as matching field in the state"
76 }
77 Self::EmptyField(_) => "field cannot be empty or all whitespace",
80 }
81 }
82
83 fn cause(&self) -> Option<&dyn std::error::Error> {
84 match self {
85 Self::JsonError(err) => Some(err),
86 _ => None,
87 }
88 }
89}
90
91impl PartialEq for OpenProtocolError<'_> {
92 fn eq(&self, other: &Self) -> bool {
104 match (self, other) {
105 (Self::JsonError(err1), Self::JsonError(err2)) => {
108 format!("{:?}", err1) == format!("{:?}", err2)
109 }
110 (Self::EmptyField(err1), Self::EmptyField(err2)) => err1 == err2,
113 (
114 Self::InvalidField { field: field1, value: value1, description: err1 },
115 Self::InvalidField { field: field2, value: value2, description: err2 },
116 ) => field1 == field2 && value1 == value2 && err1 == err2,
117 (Self::InconsistentState(err1), Self::InconsistentState(err2)) => err1 == err2,
118 (Self::InconsistentField(err1), Self::InconsistentField(err2)) => err1 == err2,
119 (Self::ConstraintViolated(err1), Self::ConstraintViolated(err2)) => err1 == err2,
120 _ => false,
121 }
122 }
123}
124
125impl Eq for OpenProtocolError<'_> {}
126
127impl std::convert::From<OpenProtocolError<'_>> for String {
128 fn from(error: OpenProtocolError<'_>) -> Self {
129 error.to_string()
130 }
131}