libcoap_rs/
error.rs

1// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * error.rs - CoAP error types.
4 * This file is part of the libcoap-rs crate, see the README and LICENSE files for
5 * more information and terms of use.
6 * Copyright © 2021-2023 The NAMIB Project Developers, all rights reserved.
7 * See the README as well as the LICENSE file for more information.
8 */
9
10//! Error types
11
12use std::string::FromUtf8Error;
13
14use thiserror::Error;
15
16use crate::protocol::{CoapMessageType, CoapOptionType};
17
18#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
19pub enum EndpointCreationError {
20    /// Unknown error inside of libcoap
21    #[error("CoAP endpoint creation error: unknown error in call to libcoap")]
22    Unknown,
23}
24
25#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
26pub enum ContextCreationError {
27    /// Unknown error inside of libcoap
28    #[error("CoAP context creation error: unknown error in call to libcoap")]
29    Unknown,
30}
31
32#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
33pub enum MessageCreationError {
34    /// Unknown error inside of libcoap
35    #[error("CoAP message creation error: unknown error in call to libcoap")]
36    Unknown,
37}
38
39#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
40pub enum IoProcessError {
41    /// Unknown error inside of libcoap
42    #[error("CoAP IO error: unknown error in call to libcoap")]
43    Unknown,
44}
45
46#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
47pub enum SessionGetAppDataError {
48    /// Stored application data type differs from requested type
49    #[error("CoAP application data retrieval error: wrong type")]
50    WrongType,
51}
52
53#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
54pub enum OptionCreationError {
55    /// Unknown error inside of libcoap
56    #[error("CoAP option creation error: unknown error in call to libcoap")]
57    Unknown,
58}
59
60#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
61pub enum SessionCreationError {
62    /// Unknown error inside of libcoap
63    #[error("CoAP session creation error: unknown error in call to libcoap")]
64    Unknown,
65}
66
67#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
68pub enum UnknownOptionError {
69    /// Unknown error inside of libcoap
70    #[error("CoAP option conversion error: unknown option")]
71    Unknown,
72}
73
74#[derive(Error, Debug, Clone, Eq, PartialEq)]
75pub enum OptionValueError {
76    /// Provided value for option is too short.
77    #[error("CoAP option has invalid value: too short")]
78    TooShort,
79    /// Provided value for option is too long.
80    #[error("CoAP option has invalid value: too long")]
81    TooLong,
82    /// A string value could not be converted to UTF-8.
83    #[error("CoAP option has invalid value: invalid string")]
84    StringConversion(#[from] FromUtf8Error),
85    /// Option has an illegal value.
86    #[error("CoAP option has invalid value")]
87    IllegalValue,
88}
89
90#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
91pub enum UriParsingError {
92    /// URI does not have a valid scheme for libcoap (coap, coaps, coap+tcp, coaps+tcp, http, https).
93    #[error("URL does not have scheme valid for libcoap")]
94    NotACoapScheme,
95}
96
97#[derive(Error, Debug, Clone, Eq, PartialEq)]
98pub enum MessageConversionError {
99    /// Value of an option is invalid.
100    #[error("CoAP message conversion error: invalid option value for {:?}", .0)]
101    InvalidOptionValue(Option<CoapOptionType>, #[source] OptionValueError),
102    /// Message has an option that is specific for another message type (i.e., request option in
103    /// response message).
104    #[error("CoAP message conversion error: option of type {:?} invalid for message type", .0)]
105    InvalidOptionForMessageType(CoapOptionType),
106    /// Non-repeatable option was repeated.
107    #[error("CoAP message conversion error: non-repeatable option of type {:?} repeated", .0)]
108    NonRepeatableOptionRepeated(CoapOptionType),
109    /// Provided URI has invalid scheme.
110    #[error("CoAP message conversion error: provided uri does not have scheme valid for CoAP")]
111    NotACoapUri(UriParsingError),
112    /// URI is invalid (most likely a Proxy URI cannot be parsed as a valid URL).
113    #[error("CoAP message conversion error: invalid uri (malformed proxy URL?)")]
114    InvalidUri(url::ParseError),
115    /// Invalid message code.
116    #[error("CoAP message conversion error: invalid message code")]
117    InvalidMessageCode(#[from] MessageCodeError),
118    /// A message with code 0.00 (Empty) contains data.
119    #[error("CoAP message conversion error: empty message contains data")]
120    DataInEmptyMessage,
121    /// Message has no token.
122    #[error("CoAP message conversion error: token missing")]
123    MissingToken,
124    /// Message has no ID.
125    #[error("CoAP message conversion error: message id missing")]
126    MissingMessageId,
127    /// Two (or more) options were combined which must not be combined (e.g., Proxy-Scheme and
128    /// Proxy-URI).
129    #[error("CoAP message conversion error: options {:?} and {:?} cannot be combined", .0, .1)]
130    InvalidOptionCombination(CoapOptionType, CoapOptionType),
131    /// A critical option (as defined in [RFC 7252](https://datatracker.ietf.org/doc/html/rfc7252#section-5.4.1)
132    /// was not recognized).
133    #[error("CoAP option identified as critical but not recognized")]
134    CriticalOptionUnrecognized,
135    /// Unknown error inside of libcoap.
136    #[error("unknown CoAP message conversion error")]
137    Unknown,
138}
139
140impl From<UriParsingError> for MessageConversionError {
141    fn from(v: UriParsingError) -> Self {
142        MessageConversionError::NotACoapUri(v)
143    }
144}
145
146impl From<url::ParseError> for MessageConversionError {
147    fn from(v: url::ParseError) -> Self {
148        MessageConversionError::InvalidUri(v)
149    }
150}
151
152#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
153pub enum MessageCodeError {
154    /// Provided message code for request was not a request code.
155    #[error("CoAP message code conversion error: not a request code")]
156    NotARequestCode,
157    /// Provided message code for response was not a response code.
158    #[error("CoAP message code conversion error: not a response code")]
159    NotAResponseCode,
160}
161
162#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
163pub enum MessageTypeError {
164    /// Message type cannot be used for this message code (e.g., ACK for request).
165    #[error("message type {:?} cannot be used for this message code", .0)]
166    InvalidForMessageCode(CoapMessageType),
167}