diff --git a/proxmox-router/Cargo.toml b/proxmox-router/Cargo.toml index 34a1985f..7d4d32f8 100644 --- a/proxmox-router/Cargo.toml +++ b/proxmox-router/Cargo.toml @@ -26,6 +26,7 @@ rustyline = { version = "9", optional = true } libc = { workspace = true, optional = true } proxmox-lang.workspace = true +proxmox-http-error.workspace = true proxmox-schema.workspace = true proxmox-async.workspace = true diff --git a/proxmox-router/src/error.rs b/proxmox-router/src/error.rs index e285cf7e..15c5417f 100644 --- a/proxmox-router/src/error.rs +++ b/proxmox-router/src/error.rs @@ -1,44 +1,4 @@ -use std::fmt; +pub use proxmox_http_error::{http_bail, http_err, HttpError}; #[doc(hidden)] pub use http::StatusCode; - -/// HTTP error including `StatusCode` and message. -#[derive(Debug)] -pub struct HttpError { - pub code: StatusCode, - pub message: String, -} - -impl std::error::Error for HttpError {} - -impl HttpError { - pub fn new(code: StatusCode, message: String) -> Self { - HttpError { code, message } - } -} - -impl fmt::Display for HttpError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.message) - } -} - -/// Macro to create a HttpError inside a anyhow::Error -#[macro_export] -macro_rules! http_err { - ($status:ident, $($fmt:tt)+) => {{ - ::anyhow::Error::from($crate::HttpError::new( - $crate::error::StatusCode::$status, - format!($($fmt)+) - )) - }}; -} - -/// Bail with an error generated with the `http_err!` macro. -#[macro_export] -macro_rules! http_bail { - ($status:ident, $($fmt:tt)+) => {{ - return Err($crate::http_err!($status, $($fmt)+)); - }}; -} diff --git a/proxmox-router/src/lib.rs b/proxmox-router/src/lib.rs index 7f4f2d97..808836a8 100644 --- a/proxmox-router/src/lib.rs +++ b/proxmox-router/src/lib.rs @@ -17,7 +17,7 @@ mod serializable_return; #[doc(inline)] #[cfg(feature = "server")] -pub use error::HttpError; +pub use error::*; pub use permission::*; pub use router::*;