1 use thiserror::Error; 2 3 use std::io; 4 use std::string::FromUtf8Error; 5 use tokio::sync::mpsc::error::SendError as MpscSendError; 6 7 pub type Result<T> = std::result::Result<T, Error>; 8 9 #[derive(Debug, Error, PartialEq)] 10 #[non_exhaustive] 11 pub enum Error { 12 #[error("attribute not found")] 13 ErrAttributeNotFound, 14 #[error("transaction is stopped")] 15 ErrTransactionStopped, 16 #[error("transaction not exists")] 17 ErrTransactionNotExists, 18 #[error("transaction exists with same id")] 19 ErrTransactionExists, 20 #[error("agent is closed")] 21 ErrAgentClosed, 22 #[error("transaction is timed out")] 23 ErrTransactionTimeOut, 24 #[error("no default reason for ErrorCode")] 25 ErrNoDefaultReason, 26 #[error("unexpected EOF")] 27 ErrUnexpectedEof, 28 #[error("attribute size is invalid")] 29 ErrAttributeSizeInvalid, 30 #[error("attribute size overflow")] 31 ErrAttributeSizeOverflow, 32 #[error("attempt to decode to nil message")] 33 ErrDecodeToNil, 34 #[error("unexpected EOF: not enough bytes to read header")] 35 ErrUnexpectedHeaderEof, 36 #[error("integrity check failed")] 37 ErrIntegrityMismatch, 38 #[error("fingerprint check failed")] 39 ErrFingerprintMismatch, 40 #[error("FINGERPRINT before MESSAGE-INTEGRITY attribute")] 41 ErrFingerprintBeforeIntegrity, 42 #[error("bad UNKNOWN-ATTRIBUTES size")] 43 ErrBadUnknownAttrsSize, 44 #[error("invalid length of IP value")] 45 ErrBadIpLength, 46 #[error("no connection provided")] 47 ErrNoConnection, 48 #[error("client is closed")] 49 ErrClientClosed, 50 #[error("no agent is set")] 51 ErrNoAgent, 52 #[error("collector is closed")] 53 ErrCollectorClosed, 54 #[error("unsupported network")] 55 ErrUnsupportedNetwork, 56 #[error("invalid url")] 57 ErrInvalidUrl, 58 #[error("unknown scheme type")] 59 ErrSchemeType, 60 #[error("invalid hostname")] 61 ErrHost, 62 #[error("{0}")] 63 Other(String), 64 #[error("url parse: {0}")] 65 Url(#[from] url::ParseError), 66 #[error("utf8: {0}")] 67 Utf8(#[from] FromUtf8Error), 68 #[error("{0}")] 69 Io(#[source] IoError), 70 #[error("mpsc send: {0}")] 71 MpscSend(String), 72 #[error("{0}")] 73 Util(#[from] util::Error), 74 } 75 76 #[derive(Debug, Error)] 77 #[error("io error: {0}")] 78 pub struct IoError(#[from] pub io::Error); 79 80 // Workaround for wanting PartialEq for io::Error. 81 impl PartialEq for IoError { eq(&self, other: &Self) -> bool82 fn eq(&self, other: &Self) -> bool { 83 self.0.kind() == other.0.kind() 84 } 85 } 86 87 impl From<io::Error> for Error { from(e: io::Error) -> Self88 fn from(e: io::Error) -> Self { 89 Error::Io(IoError(e)) 90 } 91 } 92 93 // Because Tokio SendError is parameterized, we sadly lose the backtrace. 94 impl<T> From<MpscSendError<T>> for Error { from(e: MpscSendError<T>) -> Self95 fn from(e: MpscSendError<T>) -> Self { 96 Error::MpscSend(e.to_string()) 97 } 98 } 99