Skip to main content

ldap_client/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use ldap_client_proto::ResultCode;
4
5#[derive(Debug, thiserror::Error)]
6pub enum Error {
7    #[error("I/O error: {0}")]
8    Io(#[from] std::io::Error),
9
10    #[error("BER error: {0}")]
11    Ber(#[from] ldap_client_ber::BerError),
12
13    #[error("protocol error: {0}")]
14    Proto(#[from] ldap_client_proto::ProtoError),
15
16    #[error("TLS error: {0}")]
17    Tls(#[from] rustls::Error),
18
19    #[error("invalid URL: {0}")]
20    InvalidUrl(String),
21
22    #[error("LDAP error: {code} - {message}")]
23    Ldap {
24        code: ResultCode,
25        message: String,
26        matched_dn: String,
27    },
28
29    #[error("referral to {}", urls.join(", "))]
30    Referral {
31        result: ldap_client_proto::LdapResult,
32        urls: Vec<String>,
33    },
34
35    #[error("connection closed")]
36    ConnectionClosed,
37
38    #[error("timeout")]
39    Timeout,
40
41    #[error("StartTLS failed: {0}")]
42    StartTls(String),
43
44    #[error("search returned multiple results when at most one was expected")]
45    MultipleResults,
46
47    #[error("referral hop limit exceeded")]
48    ReferralHopLimitExceeded,
49
50    #[error("search result entry limit exceeded ({0})")]
51    SearchEntryLimitExceeded(usize),
52}
53
54impl Error {
55    pub fn ldap(result: &ldap_client_proto::LdapResult) -> Self {
56        Self::Ldap {
57            code: result.code,
58            message: result.diagnostic_message.clone(),
59            matched_dn: result.matched_dn.clone(),
60        }
61    }
62
63    pub fn result_code(&self) -> Option<ResultCode> {
64        match self {
65            Self::Ldap { code, .. } => Some(*code),
66            Self::Referral { result, .. } => Some(result.code),
67            _ => None,
68        }
69    }
70}