From 5d0d56f50ba2b69a6d00379d446792e063da9a4f Mon Sep 17 00:00:00 2001 From: SamrutGadde Date: Wed, 1 May 2024 18:36:54 -0500 Subject: [PATCH] api_client: Use thiserror for errors Added thiserror crate for Error enums to the api_client package Signed-off-by: SamrutGadde --- Cargo.lock | 9 +++++---- api_client/Cargo.toml | 1 + api_client/src/lib.rs | 30 ++++++++---------------------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd47fbcfa..3f183746a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,6 +92,7 @@ checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" name = "api_client" version = "0.1.0" dependencies = [ + "thiserror", "vmm-sys-util", ] @@ -2070,18 +2071,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.60" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "579e9083ca58dd9dcf91a9923bb9054071b9ebbd800b342194c9feb0ee89fc18" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.60" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", diff --git a/api_client/Cargo.toml b/api_client/Cargo.toml index f24360b4e..95870d754 100644 --- a/api_client/Cargo.toml +++ b/api_client/Cargo.toml @@ -5,4 +5,5 @@ name = "api_client" version = "0.1.0" [dependencies] +thiserror = "1.0.61" vmm-sys-util = "0.12.1" diff --git a/api_client/src/lib.rs b/api_client/src/lib.rs index 3f87afdcb..dc264e503 100644 --- a/api_client/src/lib.rs +++ b/api_client/src/lib.rs @@ -3,41 +3,27 @@ // SPDX-License-Identifier: Apache-2.0 // -use std::fmt; use std::io::{Read, Write}; use std::os::unix::io::RawFd; +use thiserror::Error; use vmm_sys_util::sock_ctrl_msg::ScmSocket; -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { + #[error("Error writing to or reading from HTTP socket: {0}")] Socket(std::io::Error), + #[error("Error writing to or reading from HTTP socket: {0}")] SocketSendFds(vmm_sys_util::errno::Error), + #[error("Error parsing HTTP status code: {0}")] StatusCodeParsing(std::num::ParseIntError), + #[error("HTTP output is missing protocol statement")] MissingProtocol, + #[error("Error parsing HTTP Content-Length field: {0}")] ContentLengthParsing(std::num::ParseIntError), + #[error("Server responded with an error: {0:?}")] ServerResponse(StatusCode, Option), } -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use Error::*; - match self { - Socket(e) => write!(f, "Error writing to or reading from HTTP socket: {e}"), - SocketSendFds(e) => write!(f, "Error writing to or reading from HTTP socket: {e}"), - StatusCodeParsing(e) => write!(f, "Error parsing HTTP status code: {e}"), - MissingProtocol => write!(f, "HTTP output is missing protocol statement"), - ContentLengthParsing(e) => write!(f, "Error parsing HTTP Content-Length field: {e}"), - ServerResponse(s, o) => { - if let Some(o) = o { - write!(f, "Server responded with an error: {s:?}: {o}") - } else { - write!(f, "Server responded with an error: {s:?}") - } - } - } - } -} - #[derive(Clone, Copy, Debug)] pub enum StatusCode { Continue,