Skip to content

Commit

Permalink
merge: #4061
Browse files Browse the repository at this point in the history
4061: fix(sdf): Adding Status code checks for Funcs and Modules r=stack72 a=stack72



Co-authored-by: stack72 <[email protected]>
  • Loading branch information
si-bors-ng[bot] and stack72 authored Jun 28, 2024
2 parents cb074fd + 72c5141 commit 99fd846
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
18 changes: 14 additions & 4 deletions lib/sdf-server/src/server/service/func.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::server::state::AppState;
use axum::http::StatusCode;
use axum::{
response::Response,
response::{IntoResponse, Response},
routing::{get, post},
Json, Router,
};
Expand All @@ -15,8 +17,6 @@ use dal::{ChangeSetError, WsEventError};
use si_layer_cache::LayerDbError;
use thiserror::Error;

use crate::server::{impl_default_error_into_response, state::AppState};

pub mod create_attribute_prototype;
pub mod create_func;
pub mod create_func_argument;
Expand Down Expand Up @@ -78,7 +78,17 @@ pub enum FuncError {

pub type FuncResult<T> = Result<T, FuncError>;

impl_default_error_into_response!(FuncError);
impl IntoResponse for FuncError {
fn into_response(self) -> Response {
let (status, error_message) = (StatusCode::INTERNAL_SERVER_ERROR, self.to_string());

let body = Json(
serde_json::json!({ "error": { "message": error_message, "code": 42, "statusCode": status.as_u16() } }),
);

(status, body).into_response()
}
}

pub fn routes() -> Router<AppState> {
Router::new()
Expand Down
25 changes: 22 additions & 3 deletions lib/sdf-server/src/server/service/module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::server::{impl_default_error_into_response, state::AppState};
use crate::server::state::AppState;
use axum::http::StatusCode;
use axum::{
response::Response,
response::{IntoResponse, Response},
routing::{get, post},
Json, Router,
};
Expand Down Expand Up @@ -121,7 +122,25 @@ pub enum ModuleError {

pub type ModuleResult<T> = Result<T, ModuleError>;

impl_default_error_into_response!(ModuleError);
impl IntoResponse for ModuleError {
fn into_response(self) -> Response {
let (status, error_message) = match self {
ModuleError::ChangeSetNotFound(_)
| ModuleError::ModuleHashNotFound(_)
| ModuleError::PackageNotFound(_)
| ModuleError::SchemaNotFoundForVariant(_)
| ModuleError::SchemaVariantNotFound(_)
| ModuleError::WorkspaceNotFound(_) => (StatusCode::NOT_FOUND, self.to_string()),
_ => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
};

let body = Json(
serde_json::json!({ "error": { "message": error_message, "code": 42, "statusCode": status.as_u16() } }),
);

(status, body).into_response()
}
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
Expand Down

0 comments on commit 99fd846

Please sign in to comment.