1use krabka_client_admin::{AdminError, KafkaError};
2use thiserror::Error;
3
4use crate::dto::KafkaErrorDto;
5
6#[derive(Debug, Clone, PartialEq, Eq, Error)]
7pub enum UiError {
8 #[error("not authenticated")]
9 NotAuthenticated,
10 #[error("not permitted")]
13 NotPermitted,
14 #[error("invalid request: {0}")]
17 InvalidRequest(String),
18 #[error("session expired")]
19 SessionExpired,
20 #[error("broker connection failed: {0}")]
21 BrokerConnection(String),
22 #[error("broker returned error: api={api} code={code} ({name}){detail}",
23 detail = .message.as_deref().map(|message| format!(" {message:?}")).unwrap_or_default())]
24 Broker {
25 api: &'static str,
26 code: i16,
27 name: String,
28 message: Option<String>,
29 },
30 #[error("admin operation failed: {0}")]
31 Admin(String),
32}
33
34impl From<&KafkaError> for KafkaErrorDto {
35 fn from(error: &KafkaError) -> Self {
36 Self {
37 code: error.code,
38 name: error.name.to_string(),
39 message: error.message.clone(),
40 }
41 }
42}
43
44impl From<AdminError> for UiError {
45 fn from(error: AdminError) -> Self {
46 match error {
47 error @ AdminError::Connect { .. } => Self::BrokerConnection(error.to_string()),
48 AdminError::Broker {
49 api,
50 code,
51 name,
52 message,
53 } => Self::Broker {
54 api,
55 code,
56 name: name.to_string(),
57 message,
58 },
59 other => Self::Admin(other.to_string()),
60 }
61 }
62}