Skip to content

Commit

Permalink
feat: Always respond with 200 but include status key (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
petarvujovic98 authored May 18, 2024
1 parent 4c76667 commit 9c13a4f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
36 changes: 19 additions & 17 deletions host/src/interfaces/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axum::{http::StatusCode, response::IntoResponse};
use axum::response::IntoResponse;
use raiko_lib::prover::ProverError;
use utoipa::ToSchema;

Expand Down Expand Up @@ -75,23 +75,25 @@ impl From<raiko_lib::mem_db::DbError> 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()
}
}

Expand Down
10 changes: 2 additions & 8 deletions host/src/server/api/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -10,7 +10,6 @@ use tower::ServiceBuilder;
use tower_http::{
compression::CompressionLayer,
cors::{self, CorsLayer},
set_header::SetResponseHeaderLayer,
trace::TraceLayer,
};

Expand All @@ -30,12 +29,7 @@ pub fn create_router(concurrency_limit: usize) -> Router<ProverState> {
.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();

Expand Down
21 changes: 20 additions & 1 deletion host/src/server/api/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axum::Router;
use axum::{response::IntoResponse, Router};
use raiko_lib::input::GuestOutput;
use serde::Serialize;
use tower::ServiceBuilder;
Expand Down Expand Up @@ -35,6 +35,7 @@ mod proof;
crate::interfaces::error::HostError,
GuestOutputDoc,
ProofResponse,
Status,
)
),
tags(
Expand All @@ -58,6 +59,24 @@ pub struct ProofResponse {
quote: Option<String>,
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion host/src/server/api/v1/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 9c13a4f

Please sign in to comment.