Radish alpha
r
Radicle desktop app
Radicle
Git (anonymous pull)
Log in to clone via SSH
Fix error serialize in the `test-http-api` crate
Sebastian Martinez committed 1 year ago
commit 7cdf6544ad1b4b5949f3d322fe82c4369a9cf6dc
parent ac550e06029ae6ac40fb804082ab9e26d25ae5c5
7 files changed +17 -36
modified Cargo.lock
@@ -3898,6 +3898,7 @@ name = "radicle-types"
version = "0.1.0"
dependencies = [
 "anyhow",
+
 "axum",
 "base64 0.22.1",
 "localtime",
 "radicle",
modified 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"] }
modified 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<Body> {
+
        (
+
            StatusCode::INTERNAL_SERVER_ERROR,
+
            serde_json::to_string(&self).unwrap(),
+
        )
+
            .into_response()
+
    }
+
}
+

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
modified 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<radicle::Profile>,
deleted crates/test-http-api/src/error.rs
@@ -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()
-
    }
-
}
modified 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 {
modified crates/test-http-api/src/main.rs
@@ -30,6 +30,6 @@ fn parse_options() -> Result<api::Options, lexopt::Error> {
        }
    }
    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()),
    })
}