diff --git a/Cargo.toml b/Cargo.toml index d128a9c..46c254d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust-rocket-sample" -version = "0.1.1" +version = "1.0.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/errors/response.rs b/src/errors/response.rs index 8b741c4..f98b3ec 100644 --- a/src/errors/response.rs +++ b/src/errors/response.rs @@ -22,10 +22,12 @@ pub struct MyError { } impl MyError { + // building a custom error. pub fn build(code: u16, description: Option) -> MyError { let reason: String; match code { 400 => reason = "Bad Request".to_string(), + 401 => reason = "Unauthorized".to_string(), _ => reason = "Error".to_string(), } MyError { @@ -93,7 +95,8 @@ impl OpenApiResponderInner for MyError { Ok(Responses { responses: okapi::map! { "400".to_owned() => RefOr::Object(bad_request_response(gen)), - "401".to_owned() => RefOr::Object(unauthorized_response(gen)), + // Note: 401 is already declared for ApiKey. so this is not essential. + // "401".to_owned() => RefOr::Object(unauthorized_response(gen)), }, ..Default::default() }) diff --git a/src/request_guards/basic.rs b/src/request_guards/basic.rs index 18c285b..9321ac4 100644 --- a/src/request_guards/basic.rs +++ b/src/request_guards/basic.rs @@ -10,7 +10,7 @@ use rocket_okapi::{ }; use std::env; -use crate::errors::response::{bad_request_response, unauthorized_response}; +use crate::errors::response::unauthorized_response; // #[derive(OpenApiFromRequest)] pub struct ApiKey(String); @@ -61,7 +61,7 @@ impl<'a> OpenApiFromRequest<'a> for ApiKey { // Each security requirement needs to be met before access is allowed. security_req.insert("ApiKey".to_owned(), Vec::new()); Ok(RequestHeaderInput::Security( - "ApiKeyAuth".to_owned(), + "ApiKey".to_owned(), security_scheme, security_req, )) @@ -71,7 +71,6 @@ impl<'a> OpenApiFromRequest<'a> for ApiKey { use rocket_okapi::okapi::openapi3::RefOr; Ok(Responses { responses: okapi::map! { - "400".to_owned() => RefOr::Object(bad_request_response(gen)), "401".to_owned() => RefOr::Object(unauthorized_response(gen)), }, ..Default::default() diff --git a/src/routes/customer.rs b/src/routes/customer.rs index 92f8fa0..99e8cf1 100644 --- a/src/routes/customer.rs +++ b/src/routes/customer.rs @@ -21,30 +21,30 @@ use crate::errors::response::MyError; #[get("/customer?&")] pub async fn get_customers( db: &State, - limit: i64, + limit: Option, page: Option, -) -> Result>, BadRequest>> { +) -> Result>, MyError> { // Error handling - if limit < 0 { - return Err(BadRequest(Some(Json(MessageResponse { - message: "limit cannot be less than 0".to_string(), - })))); - } - - if !page.is_none() && page.unwrap() < 1 { - return Err(BadRequest(Some(Json(MessageResponse { - message: "page cannot be less than 1".to_string(), - })))); - } - - match customer::find_customer(&db, limit, if page.is_none() { 1 } else { page.unwrap() }).await - { + // This is also valid when strict checking is necessary. + // if limit < 0 { + // return Err(BadRequest(Some(Json(MessageResponse { + // message: "limit cannot be less than 0".to_string(), + // })))); + // } + // if !page.is_none() && page.unwrap() < 1 { + // return Err(BadRequest(Some(Json(MessageResponse { + // message: "page cannot be less than 1".to_string(), + // })))); + // } + + // Setting default values + let limit: i64 = limit.unwrap_or(12); + let page: i64 = page.unwrap_or(1); + match customer::find_customer(&db, limit, page).await { Ok(_customer_docs) => Ok(Json(_customer_docs)), Err(_error) => { println!("{:?}", _error); - Err(BadRequest(Some(Json(MessageResponse { - message: _error.to_string(), - })))) + return Err(MyError::build(400, Some(_error.to_string()))); } } } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index d6ac918..010b19d 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -6,7 +6,7 @@ use crate::models::response::MessageResponse; pub mod customer; /// This is a description.
You can do simple html
like this -#[openapi(tag = "Hello")] +#[openapi(tag = "Hello World")] #[get("/")] pub fn index() -> Json { Json(MessageResponse {