metio/client/error.rs
1/*
2 * Copyright 2024 Bagaluten GmbH <contact@bagaluten.email>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#[derive(Clone, Debug)]
18pub enum Kind {
19 /// This indicates that something with the given client configuration is not correct.
20 /// This could be a missing field or a wrong value.
21 Config,
22 /// This indicates that the client could not connect to the server.
23 Connect,
24 /// This indicates that the client could not send the message.
25 /// If this error occurs the messages that were not able to be sent are provided
26 /// in the `related_messages` field.
27 Send,
28}
29
30impl std::fmt::Display for Kind {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 match self {
33 Kind::Config => write!(f, "Config"),
34 Kind::Connect => write!(f, "Connect"),
35 Kind::Send => write!(f, "Send"),
36 }
37 }
38}
39
40#[derive(Debug, Clone)]
41pub struct Error {
42 kind: Kind,
43 message: String,
44 related_messages: Option<Vec<String>>,
45}
46
47impl Error {
48 pub fn new(kind: Kind, message: String) -> Self {
49 Self {
50 kind,
51 message,
52 related_messages: None,
53 }
54 }
55
56 pub fn new_with_related(kind: Kind, message: String, related_message: Vec<String>) -> Self {
57 Self {
58 kind,
59 message,
60 related_messages: Some(related_message),
61 }
62 }
63}
64
65impl std::fmt::Display for Error {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 if let Some(related) = &self.related_messages {
68 write!(f, "{}: {}\nRelated: {:?}", self.kind, self.message, related)
69 } else {
70 write!(f, "{}: {}", self.kind, self.message)
71 }
72 }
73}