Internal error type did not implement the necessary traits to be
treated as a proper Rust error. This would cause ergonomic issues
with manual error handling and with error handling crates like
anyhow. Implement the Display and Error trait for internal Error
type. This also fixes the build warnings.
Signed-off-by: Jacob Reger <regerjacob@gmail.com>
---
rust/src/error.rs | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/rust/src/error.rs b/rust/src/error.rs
index ce444e199..f3709516c 100644
--- a/rust/src/error.rs
+++ b/rust/src/error.rs
@@ -46,6 +46,42 @@ pub enum Error {
Create,
}
+impl std::fmt::Display for Error {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ Error::API(err) => {
+ write!(
+ f,
+ "API Error:\n\tOperation: {}\n\tMessage: {}\n\tError Number: {}",
+ err.operation, err.message, err.errno
+ )
+ }
+ Error::IllegalString(err) => {
+ write!(
+ f,
+ "Illegal string Error:\nNull byte found\n\tDetails: {}",
+ err
+ )
+ }
+ Error::Utf8Error(err) => {
+ write!(
+ f,
+ "Utf8 Error:\nFailed to interpret string as utf-8\n\tDetails: {}",
+ err
+ )
+ }
+ Error::UnixError(err, op) => {
+ write!(f, "Unix Error:\n\tError: {}\n\tOperation: {}", err, op)
+ }
+ Error::Create => {
+ write!(f, "Creation Error:\nFailed to create a guestfs handle")
+ }
+ }
+ }
+}
+
+impl std::error::Error for Error {}
+
impl convert::From<ffi::NulError> for Error {
fn from(error: ffi::NulError) -> Self {
Error::IllegalString(error)
--
2.39.5 (Apple Git-154)