api_client: Use thiserror for errors

Added thiserror crate for Error enums to the api_client package

Signed-off-by: SamrutGadde <samrut.gadde@gmail.com>
This commit is contained in:
SamrutGadde 2024-05-01 18:36:54 -05:00 committed by Bo Chen
parent 30b6e412af
commit 5d0d56f50b
3 changed files with 14 additions and 26 deletions

9
Cargo.lock generated
View File

@ -92,6 +92,7 @@ checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247"
name = "api_client" name = "api_client"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"thiserror",
"vmm-sys-util", "vmm-sys-util",
] ]
@ -2070,18 +2071,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.60" version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "579e9083ca58dd9dcf91a9923bb9054071b9ebbd800b342194c9feb0ee89fc18" checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.60" version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524" checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -5,4 +5,5 @@ name = "api_client"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
thiserror = "1.0.61"
vmm-sys-util = "0.12.1" vmm-sys-util = "0.12.1"

View File

@ -3,41 +3,27 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use std::fmt;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::os::unix::io::RawFd; use std::os::unix::io::RawFd;
use thiserror::Error;
use vmm_sys_util::sock_ctrl_msg::ScmSocket; use vmm_sys_util::sock_ctrl_msg::ScmSocket;
#[derive(Debug)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
#[error("Error writing to or reading from HTTP socket: {0}")]
Socket(std::io::Error), Socket(std::io::Error),
#[error("Error writing to or reading from HTTP socket: {0}")]
SocketSendFds(vmm_sys_util::errno::Error), SocketSendFds(vmm_sys_util::errno::Error),
#[error("Error parsing HTTP status code: {0}")]
StatusCodeParsing(std::num::ParseIntError), StatusCodeParsing(std::num::ParseIntError),
#[error("HTTP output is missing protocol statement")]
MissingProtocol, MissingProtocol,
#[error("Error parsing HTTP Content-Length field: {0}")]
ContentLengthParsing(std::num::ParseIntError), ContentLengthParsing(std::num::ParseIntError),
#[error("Server responded with an error: {0:?}")]
ServerResponse(StatusCode, Option<String>), ServerResponse(StatusCode, Option<String>),
} }
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)] #[derive(Clone, Copy, Debug)]
pub enum StatusCode { pub enum StatusCode {
Continue, Continue,