Skip to content

Commit

Permalink
handle invald payload Json reqeust with rejection schema response
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Jan 11, 2024
1 parent 90eee31 commit ccf6dc4
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ rusty-sidekiq = { version = "0.8.2", default-features = false }
async-trait = "0.1.74"
bb8 = "0.8.1"

axum = "0.7.1"
axum = { version = "0.7.1", features = ["macros"] }
axum-extra = { version = "0.9", features = ["cookie"] }
regex = "1"
lazy_static = "1.4.0"
Expand Down
13 changes: 13 additions & 0 deletions examples/demo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions src/controller/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
//! Rust struct.
//!
//! ```rust
//! use loco_rs::{controller::format, Result};
//! use axum::Json;
//! use loco_rs::{controller::{Json, format}, Result};
//!
//! pub struct Health {
//! pub ok: bool,
Expand All @@ -20,19 +19,17 @@
//! }
//! ```
use crate::{controller::Json, Result};
use axum::{
body::Body,
http::{response::Builder, HeaderName, HeaderValue},
response::{Html, Response},
Json,
};
use axum_extra::extract::cookie::Cookie;
use bytes::{BufMut, BytesMut};
use hyper::{header, StatusCode};
use serde::Serialize;

use crate::Result;

/// Returns an empty response.
///
/// # Example:
Expand Down Expand Up @@ -84,10 +81,9 @@ pub fn text(t: &str) -> Result<String> {
///
/// ```rust
/// use loco_rs::{
/// controller::format,
/// controller::{Json, format},
/// Result,
/// };
/// use axum::Json;
///
/// pub struct Health {
/// pub ok: bool,
Expand Down
4 changes: 2 additions & 2 deletions src/controller/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//! reporting. These routes are commonly used to monitor the health of the
//! application and its dependencies.
use axum::{extract::State, routing::get, Json};
use axum::{extract::State, routing::get};
use serde::Serialize;

use super::{format, routes::Routes};
use crate::{app::AppContext, redis, Result};
use crate::{app::AppContext, controller::Json, redis, Result};

/// Represents the health status of the application.
#[derive(Serialize)]
Expand Down
15 changes: 12 additions & 3 deletions src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ pub use app_routes::{AppRoutes, ListRoutes};
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use colored::Colorize;
pub use routes::Routes;
Expand All @@ -98,10 +97,9 @@ use crate::{errors::Error, Result};
/// # Example
///
/// ```rust
/// use axum::Json;
/// use loco_rs::{
/// Result,
/// controller::{format, unauthorized}
/// controller::{format, Json, unauthorized}
/// };
///
/// async fn login() -> Result<Json<()>> {
Expand Down Expand Up @@ -153,6 +151,17 @@ impl ErrorDetail {
}
}

use axum::extract::FromRequest;
#[derive(Debug, FromRequest)]
#[from_request(via(axum::Json), rejection(Error))]
pub struct Json<T>(pub T);

impl<T: Serialize> IntoResponse for Json<T> {
fn into_response(self) -> axum::response::Response {
axum::Json(self.0).into_response()
}
}

impl IntoResponse for Error {
/// Convert an `Error` into an HTTP response.
fn into_response(self) -> Response {
Expand Down
4 changes: 2 additions & 2 deletions src/controller/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//! reporting. These routes are commonly used to monitor the health of the
//! application and its dependencies.
use axum::{routing::get, Json};
use axum::routing::get;
use serde::Serialize;

use super::{format, routes::Routes};
use crate::Result;
use crate::{controller::Json, Result};

/// Represents the health status of the application.
#[derive(Serialize)]
Expand Down
14 changes: 10 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! # Application Error Handling
use axum::http::{
header::{InvalidHeaderName, InvalidHeaderValue},
method::InvalidMethod,
StatusCode,
use axum::{
extract::rejection::JsonRejection,
http::{
header::{InvalidHeaderName, InvalidHeaderValue},
method::InvalidMethod,
StatusCode,
},
};
use lettre::{address::AddressError, transport::smtp};

Expand Down Expand Up @@ -47,6 +50,9 @@ pub enum Error {
#[error(transparent)]
JSON(serde_json::Error),

#[error(transparent)]
JsonRejection(#[from] JsonRejection),

#[error("cannot parse `{1}`: {0}")]
YAMLFile(#[source] serde_yaml::Error, String),

Expand Down
3 changes: 1 addition & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub use async_trait::async_trait;
pub use axum::{
extract::{Form, Path, State},
routing::{delete, get, post, put},
Json,
};
pub use axum_extra::extract::cookie;
pub use chrono::NaiveDateTime as DateTime;
Expand All @@ -14,7 +13,7 @@ pub use sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveModel, ModelTrait, Se
pub use crate::controller::middleware::auth;
pub use crate::{
app::AppContext,
controller::{format, not_found, unauthorized, Routes},
controller::{format, not_found, unauthorized, Json, Routes},
errors::Error,
mailer,
mailer::Mailer,
Expand Down

0 comments on commit ccf6dc4

Please sign in to comment.