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(),
}
}
}