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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use alloc::{
borrow::ToOwned,
string::{String, ToString},
};
use core::{fmt, num::TryFromIntError};
#[cfg(feature = "std")]
use std::error;
use crate::ResponseType;
#[derive(Debug, PartialEq)]
pub enum MessageError {
InvalidHeader,
InvalidPacketLength,
InvalidTokenLength,
InvalidOptionDelta,
InvalidOptionLength,
}
impl fmt::Display for MessageError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
MessageError::InvalidHeader => {
write!(f, "CoAP error: invalid header")
}
MessageError::InvalidPacketLength => {
write!(f, "CoAP error: invalid packet length, consider using BlockHandler")
}
MessageError::InvalidTokenLength => {
write!(f, "CoAP error: invalid token length")
}
MessageError::InvalidOptionDelta => {
write!(f, "CoAP error: invalid option delta")
}
MessageError::InvalidOptionLength => {
write!(f, "CoAP error: invalid option length")
}
}
}
}
#[cfg(feature = "std")]
impl error::Error for MessageError {}
#[derive(Debug, PartialEq)]
pub struct InvalidContentFormat;
impl fmt::Display for InvalidContentFormat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CoAP error: invalid content-format number")
}
}
#[cfg(feature = "std")]
impl error::Error for InvalidContentFormat {}
#[derive(Debug, PartialEq)]
pub struct InvalidObserve;
impl fmt::Display for InvalidObserve {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CoAP error: invalid observe option number")
}
}
#[cfg(feature = "std")]
impl error::Error for InvalidObserve {}
#[derive(Debug, PartialEq)]
pub struct IncompatibleOptionValueFormat {
pub message: String,
}
impl fmt::Display for IncompatibleOptionValueFormat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Incompatible option value: {}", self.message)
}
}
#[cfg(feature = "std")]
impl error::Error for IncompatibleOptionValueFormat {}
#[derive(Debug, PartialEq)]
pub enum InvalidBlockValue {
SizeExponentEncodingError(usize),
TypeBoundsError(TryFromIntError),
}
impl fmt::Display for InvalidBlockValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InvalidBlockValue::SizeExponentEncodingError(size) => {
write!(f, "size cannot be encoded {}", size)
}
InvalidBlockValue::TypeBoundsError(err) => {
write!(f, "size provided is outside type bounds: {}", err)
}
}
}
}
#[cfg(feature = "std")]
impl error::Error for InvalidBlockValue {}
#[derive(Debug, Clone)]
pub struct HandlingError {
pub code: Option<ResponseType>,
pub message: String,
}
impl fmt::Display for HandlingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Handling error {:?}: {}", self.code, self.message)
}
}
#[cfg(feature = "std")]
impl error::Error for HandlingError {}
impl HandlingError {
pub fn not_handled() -> Self {
Self {
code: None,
message: "Not handled".to_owned(),
}
}
pub fn not_found() -> Self {
Self::with_code(ResponseType::NotFound, "Not found")
}
pub fn bad_request<T: ToString>(e: T) -> Self {
Self::with_code(ResponseType::BadRequest, e)
}
pub fn internal<T: ToString>(e: T) -> Self {
Self::with_code(ResponseType::InternalServerError, e)
}
pub fn method_not_supported() -> Self {
Self::with_code(ResponseType::MethodNotAllowed, "Method not supported")
}
pub fn with_code<T: ToString>(code: ResponseType, e: T) -> Self {
Self {
code: Some(code),
message: e.to_string(),
}
}
}