From 9c13a4fcb5e466ff0e190b52160c5d42b71f67e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Vujovi=C4=87?= Date: Sat, 18 May 2024 08:37:51 +0200 Subject: [PATCH] feat: Always respond with 200 but include status key (#214) --- host/src/interfaces/error.rs | 36 +++++++++++++++++---------------- host/src/server/api/mod.rs | 10 ++------- host/src/server/api/v1/mod.rs | 21 ++++++++++++++++++- host/src/server/api/v1/proof.rs | 2 +- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/host/src/interfaces/error.rs b/host/src/interfaces/error.rs index fda0b565..46cea8ea 100644 --- a/host/src/interfaces/error.rs +++ b/host/src/interfaces/error.rs @@ -1,4 +1,4 @@ -use axum::{http::StatusCode, response::IntoResponse}; +use axum::response::IntoResponse; use raiko_lib::prover::ProverError; use utoipa::ToSchema; @@ -75,23 +75,25 @@ impl From for HostError { impl IntoResponse for HostError { fn into_response(self) -> axum::response::Response { - use HostError::*; - match self { - InvalidProofType(e) | InvalidRequestConfig(e) | InvalidAddress(e) => { - (StatusCode::BAD_REQUEST, e.to_string()).into_response() + let (error, message) = match self { + HostError::InvalidProofType(e) => ("invalid_proof_type".to_string(), e), + HostError::InvalidRequestConfig(e) => ("invalid_request_config".to_string(), e), + HostError::InvalidAddress(e) => ("invalid_address".to_string(), e), + HostError::Io(e) => ("io_error".to_string(), e.to_string()), + HostError::Preflight(e) => ("preflight_error".to_string(), e), + HostError::Conversion(e) => ("conversion_error".to_string(), e), + HostError::RPC(e) => ("rpc_error".to_string(), e), + HostError::Serde(e) => ("serde_error".to_string(), e.to_string()), + HostError::JoinHandle(e) => ("join_handle_error".to_string(), e.to_string()), + HostError::Guest(e) => ("guest_error".to_string(), e.to_string()), + HostError::Db(e) => ("db_error".to_string(), e.to_string()), + HostError::FeatureNotSupportedError(t) => { + ("feature_not_supported_error".to_string(), t.to_string()) } - Conversion(e) | Preflight(e) => (StatusCode::INTERNAL_SERVER_ERROR, e).into_response(), - Io(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(), - Serde(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(), - Anyhow(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(), - JoinHandle(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(), - Guest(e) => (StatusCode::FAILED_DEPENDENCY, e.to_string()).into_response(), - RPC(e) => (StatusCode::FAILED_DEPENDENCY, e.to_string()).into_response(), - Db(e) => (StatusCode::FAILED_DEPENDENCY, e.to_string()).into_response(), - FeatureNotSupportedError(e) => { - (StatusCode::METHOD_NOT_ALLOWED, e.to_string()).into_response() - } - } + HostError::Anyhow(e) => ("anyhow_error".to_string(), e.to_string()), + }; + axum::Json(serde_json::json!({ "status": "error", "error": error, "message": message })) + .into_response() } } diff --git a/host/src/server/api/mod.rs b/host/src/server/api/mod.rs index 8e9c379a..ccc29104 100644 --- a/host/src/server/api/mod.rs +++ b/host/src/server/api/mod.rs @@ -1,7 +1,7 @@ use axum::{ body::HttpBody, extract::Request, - http::{header, HeaderName, HeaderValue, Method, StatusCode, Uri}, + http::{header, HeaderName, Method, StatusCode, Uri}, middleware::{self, Next}, response::Response, Router, @@ -10,7 +10,6 @@ use tower::ServiceBuilder; use tower_http::{ compression::CompressionLayer, cors::{self, CorsLayer}, - set_header::SetResponseHeaderLayer, trace::TraceLayer, }; @@ -30,12 +29,7 @@ pub fn create_router(concurrency_limit: usize) -> Router { .allow_origin(cors::Any); let compression = CompressionLayer::new(); - let middleware = ServiceBuilder::new().layer(cors).layer(compression).layer( - SetResponseHeaderLayer::overriding( - header::CONTENT_TYPE, - HeaderValue::from_static("application/json"), - ), - ); + let middleware = ServiceBuilder::new().layer(cors).layer(compression); let trace = TraceLayer::new_for_http(); diff --git a/host/src/server/api/v1/mod.rs b/host/src/server/api/v1/mod.rs index 7dacb25c..dbff5404 100644 --- a/host/src/server/api/v1/mod.rs +++ b/host/src/server/api/v1/mod.rs @@ -1,4 +1,4 @@ -use axum::Router; +use axum::{response::IntoResponse, Router}; use raiko_lib::input::GuestOutput; use serde::Serialize; use tower::ServiceBuilder; @@ -35,6 +35,7 @@ mod proof; crate::interfaces::error::HostError, GuestOutputDoc, ProofResponse, + Status, ) ), tags( @@ -58,6 +59,24 @@ pub struct ProofResponse { quote: Option, } +impl IntoResponse for ProofResponse { + fn into_response(self) -> axum::response::Response { + axum::Json(serde_json::json!({ + "status": "ok", + "data": self + })) + .into_response() + } +} + +#[derive(Debug, Serialize, ToSchema)] +#[serde(tag = "status", rename_all = "lowercase")] +#[allow(dead_code)] +pub enum Status { + Ok { data: ProofResponse }, + Error { error: String, message: String }, +} + #[derive(Debug, Serialize, ToSchema)] #[allow(dead_code)] pub enum GuestOutputDoc { diff --git a/host/src/server/api/v1/proof.rs b/host/src/server/api/v1/proof.rs index 93f8e78d..e766a314 100644 --- a/host/src/server/api/v1/proof.rs +++ b/host/src/server/api/v1/proof.rs @@ -68,7 +68,7 @@ fn dec_concurrent_req_count(e: HostError) -> HostError { tag = "Proving", request_body = ProofRequestOpt, responses ( - (status = 200, description = "Successfully created proof for request", body = ProofResponse) + (status = 200, description = "Successfully created proof for request", body = Status) ) )] #[debug_handler(state = ProverState)]