diff --git a/Cargo.lock b/Cargo.lock index 60e7df698..bcee14a1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3898,6 +3898,7 @@ name = "radicle-types" version = "0.1.0" dependencies = [ "anyhow", + "axum", "base64 0.22.1", "localtime", "radicle", diff --git a/crates/radicle-types/Cargo.toml b/crates/radicle-types/Cargo.toml index 546160c04..10a5fb70e 100644 --- a/crates/radicle-types/Cargo.toml +++ b/crates/radicle-types/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] anyhow = { version = "1.0.90" } +axum = { version = "0.7.5", default-features = false, features = ["json"] } base64 = { version = "0.22.1" } radicle = { git = "https://seed.radicle.xyz/z3gqcJUoA1n9HaHKufZs5FCSGazv5.git" } radicle-surf = { version = "0.22.1", features = ["serde"] } diff --git a/crates/radicle-types/src/error.rs b/crates/radicle-types/src/error.rs index e7ea7624a..20df15a77 100644 --- a/crates/radicle-types/src/error.rs +++ b/crates/radicle-types/src/error.rs @@ -1,3 +1,6 @@ +use axum::body::Body; +use axum::http::{Response, StatusCode}; +use axum::response::IntoResponse; use serde::Serialize; #[derive(Debug, thiserror::Error)] @@ -108,6 +111,16 @@ impl Serialize for Error { } } +impl IntoResponse for Error { + fn into_response(self) -> Response { + ( + StatusCode::INTERNAL_SERVER_ERROR, + serde_json::to_string(&self).unwrap(), + ) + .into_response() + } +} + #[cfg(test)] #[allow(clippy::unwrap_used)] mod test { diff --git a/crates/test-http-api/src/api.rs b/crates/test-http-api/src/api.rs index ecabc88cc..bc7bbeaa8 100644 --- a/crates/test-http-api/src/api.rs +++ b/crates/test-http-api/src/api.rs @@ -15,6 +15,7 @@ use radicle::{git, identity}; use radicle_types as types; use radicle_types::cobs::issue::{Action, NewIssue}; use radicle_types::cobs::CobOptions; +use radicle_types::error::Error; use radicle_types::traits::auth::Auth; use radicle_types::traits::cobs::Cobs; use radicle_types::traits::issue::{Issues, IssuesMut}; @@ -23,8 +24,6 @@ use radicle_types::traits::repo::Repo; use radicle_types::traits::thread::Thread; use radicle_types::traits::Profile; -use crate::error::Error; - #[derive(Clone)] pub struct Context { profile: Arc, diff --git a/crates/test-http-api/src/error.rs b/crates/test-http-api/src/error.rs deleted file mode 100644 index 5cc9103b8..000000000 --- a/crates/test-http-api/src/error.rs +++ /dev/null @@ -1,32 +0,0 @@ -use axum::http::StatusCode; -use axum::response::{IntoResponse, Response}; -use axum::Json; -use serde_json::json; - -#[derive(Debug, thiserror::Error)] -pub enum Error { - /// radicle_types error. - #[error(transparent)] - Types(#[from] radicle_types::error::Error), -} - -impl IntoResponse for Error { - fn into_response(self) -> Response { - let (status, msg) = match self { - other => { - if cfg!(debug_assertions) { - (StatusCode::INTERNAL_SERVER_ERROR, Some(other.to_string())) - } else { - (StatusCode::INTERNAL_SERVER_ERROR, None) - } - } - }; - - let body = Json(json!({ - "error": msg.or_else(|| status.canonical_reason().map(|r| r.to_string())), - "code": status.as_u16() - })); - - (status, body).into_response() - } -} diff --git a/crates/test-http-api/src/lib.rs b/crates/test-http-api/src/lib.rs index 9d66e0a1c..35d046a24 100644 --- a/crates/test-http-api/src/lib.rs +++ b/crates/test-http-api/src/lib.rs @@ -7,7 +7,6 @@ use tokio::net::TcpListener; use radicle::Profile; mod api; -mod error; #[derive(Debug, Clone)] pub struct Options { diff --git a/crates/test-http-api/src/main.rs b/crates/test-http-api/src/main.rs index 4560d3f13..d285eb2ee 100644 --- a/crates/test-http-api/src/main.rs +++ b/crates/test-http-api/src/main.rs @@ -30,6 +30,6 @@ fn parse_options() -> Result { } } Ok(api::Options { - listen: listen.unwrap_or_else(|| ([0, 0, 0, 0], 8080).into()), + listen: listen.unwrap_or_else(|| ([0, 0, 0, 0], 8081).into()), }) }