#[derive(Debug)]
pub enum BgpError {
Static(&'static str),
InsufficientBufferSize(&'static str,u32),
ProtocolError(&'static str,u32),
TooManyData(&'static str,u32),
DynStr(std::string::String),
Other(Box<dyn std::error::Error + Send + Sync>),
}
impl BgpError {
#[inline]
pub fn static_str(ms: &'static str) -> BgpError {
BgpError::Static(ms)
}
#[inline]
pub fn from_string(s: std::string::String) -> BgpError {
BgpError::DynStr(s)
}
#[inline]
pub fn from_error(e: Box<dyn std::error::Error + Send + Sync>) -> BgpError {
BgpError::Other(e)
}
}
impl std::fmt::Display for BgpError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
BgpError::InsufficientBufferSize(src,lineno) => write!(f, "BgpError InsufficientBufferSize ({}:{})", *src, *lineno),
BgpError::ProtocolError(src,lineno) => write!(f, "BgpError ProtocolError({}:{})", *src, *lineno),
BgpError::TooManyData(src,lineno) => write!(f, "BgpError TooManyData({}:{})", *src, *lineno),
BgpError::Static(s) => write!(f, "BgpError {}", s),
BgpError::DynStr(s) => write!(f, "BgpError {}", s),
BgpError::Other(e) => write!(f, "BgpError {}", e),
}
}
}
impl std::error::Error for BgpError {
fn cause(&self) -> Option<&dyn std::error::Error> {
match self {
BgpError::Other(e) => Some(e.as_ref()),
_ => None,
}
}
}
impl From<std::io::Error> for BgpError {
#[inline]
fn from(error: std::io::Error) -> Self {
BgpError::Other(Box::new(error))
}
}
impl From<std::net::AddrParseError> for BgpError {
#[inline]
fn from(error: std::net::AddrParseError) -> Self {
BgpError::Other(Box::new(error))
}
}
impl From<std::str::Utf8Error> for BgpError {
#[inline]
fn from(error: std::str::Utf8Error) -> Self {
BgpError::Other(Box::new(error))
}
}