astarte_device_sdk/transport/mqtt/error.rs
1// This file is part of Astarte.
2//
3// Copyright 2024 SECO Mind Srl
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// SPDX-License-Identifier: Apache-2.0
18
19//! Errors returned by the MQTT connection
20
21use rumqttc::{ClientError, TokenError};
22use tokio::time::error::Elapsed;
23
24use super::connection::PollError;
25use super::{PairingError, PayloadError};
26use crate::store::error::StoreError;
27use crate::transport::mqtt::topic::TopicError;
28
29/// Errors raised during construction of the [`Mqtt`](super::Mqtt) struct
30#[non_exhaustive]
31#[derive(Debug, thiserror::Error)]
32pub enum MqttError {
33 /// Error while pairing with Astarte
34 #[error("couldn't pair with Astarte")]
35 Pairing(#[from] PairingError),
36 /// Error while loading the property for the session data.
37 #[error("Error while loading session data to perform the mqtt connection: {0}")]
38 PropLoad(#[from] StoreError),
39 /// Failed to subscribe to topic
40 #[error["Couldn't subscribe to topic"]]
41 Subscribe(#[source] ClientError),
42 /// Failed to unsubscribe to topic
43 #[error["Couldn't unsubscribe to topic"]]
44 Unsubscribe(#[source] ClientError),
45 /// Failed to publish on topic
46 #[error("Couldn't publish on topic {ctx}")]
47 Publish {
48 /// The topic we tried to publish on.
49 ctx: &'static str,
50 /// Reason why the publish failed.
51 #[source]
52 backtrace: ClientError,
53 },
54 /// Errors that can occur handling the payload.
55 #[error("couldn't process payload")]
56 Payload(#[from] PayloadError),
57 /// Couldn't parse the topic
58 #[error("couldn't parse the topic")]
59 Topic(#[from] TopicError),
60 /// Couldn't authenticate with the pairing token, because we are missing a writable directory
61 ///
62 /// See the [`ParingToken`](super::Credential::ParingToken) for more information.
63 #[error("missing writable directory to store credentials to use the pairing token")]
64 NoStorePairingToken,
65 /// Couldn't poll the connection
66 #[error("couldn't poll the connection")]
67 Poll(#[from] PollError),
68 /// Couldn't send the disconnect
69 #[error("couldn't send the disconnect")]
70 Disconnect(#[source] ClientError),
71 /// Token error while waiting for ack
72 #[error("token error while waiting for ack")]
73 PubAckToken(#[source] TokenError),
74 /// The client is currently disconnected
75 #[error("no client, connection with the server was not established")]
76 NoClient,
77 /// Timeout reached
78 #[error("the configured timeout was reached {0}")]
79 Timeout(Elapsed),
80}
81
82impl MqttError {
83 pub(crate) const fn publish(ctx: &'static str, error: ClientError) -> Self {
84 Self::Publish {
85 ctx,
86 backtrace: error,
87 }
88 }
89}