burp: error: impl Display and Error

The `burp::error::Error` struct needs to implement the
`std::error::Error` trait (which requires that it also implement
`std::fmt::Display`) so that it can be used in dynamic dispatch
situations.  Notably, the `prometheus_exporter_base::render_prometheus`
function requires a closure that returns `Result<String, Box<dyn
std::error::Error>>`, so in order to use the `?` (early return)
operator on on the `metrics::get_metrics` function call, our error must
implement that trait.
master
Dustin 2022-02-12 11:31:58 -06:00
parent c7feaa041a
commit dc0c987358
1 changed files with 16 additions and 0 deletions

View File

@ -1,4 +1,6 @@
//! Client error handling //! Client error handling
use std::error;
use std::fmt;
use std::io; use std::io;
use serde_json; use serde_json;
@ -40,3 +42,17 @@ impl From<serde_json::Error> for Error {
Self::Protocol(error.to_string()) Self::Protocol(error.to_string())
} }
} }
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "I/O error: {}", e),
Self::Tls(e) => write!(f, "TLS handshake error: {}", e),
Self::Command(e) => write!(f, "Error from BURP server: {}", e),
Self::Protocol(e) => write!(f, "BURP protocol error: {}", e),
Self::UnsupportedVersion(e) => write!(f, "{}", e),
}
}
}
impl error::Error for Error {}