| Crates.io | axum-resp-result |
| lib.rs | axum-resp-result |
| version | 0.7.1 |
| created_at | 2024-02-15 09:31:02.845605+00 |
| updated_at | 2024-03-19 07:17:46.565848+00 |
| description | Help Struct For Axum Response |
| homepage | https://github.com/Goodjooy/resp-result |
| repository | |
| max_upload_size | |
| id | 1140836 |
| size | 116,078 |
Help data structure for web framework response
Result will become 500 using as a web framework response type when Err(_), the action usually not I expect?, the code will fill with if let or matchthat why I need a RespResult, which can
RespResult::Err, not always 500Try thus can use friendly ? to simplify codenote: because the
Trynot stable yet, this crate needNightlyrust
add resp-result into your crate
[dependencies]
resp-result = "*"
extra-error: enable extra error message in trait RespErrorlog: make tracing also logger to the logtracing : enable recorder using tracingnightly_try_v2 : impl Try for RespResult making it can use ?, it will enable feature try_trait_v2 and require Nightly rust compilerRespResult<T,E> require the E impl the RespError
for example
use resp_result::{RespError, RespResult};
use std::borrow::Cow;
use http::StatusCode;
pub struct PlainError(String);
impl RespError for PlainError{
fn log_message(&self) -> Cow<'_, str> {
Cow::Owned(format!("PlainError: {}", self.0))
}
fn resp_message(&self) -> Cow<'_, str> {
"PlainError".into()
}
fn http_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
type ExtraMessage = String;
fn extra_message(&self) -> Self::ExtraMessage {
self.0.clone()
}
}
/// this can be used as a handler return type
type PlainRResult<T> = RespResult<T, PlainError>;
T in RespResult<T, E>The T require implement Serialize and has 'static lifetime
the following is an example for using [RespResult]
use resp_result::{RespError, RespResult};
use std::borrow::Cow;
use http::StatusCode;
pub struct PlainError(String);
impl RespError for PlainError{
fn log_message(&self) -> Cow<'_, str> {
Cow::Owned(format!("PlainError: {}", self.0))
}
type ExtraMessage = String;
fn extra_message(&self) -> Self::ExtraMessage {
self.0.clone()
}
}
/// this can be used as a handler return type
type PlainRResult<T> = RespResult<T, PlainError>;
pub async fn welcome_short_name(name: String) -> PlainRResult<String>{
if name.len() >= 8{
// you can use `?` just like the function that returns `Result`
Err(PlainError("the name size great then 8".to_string()))?;
}
if name.len() >= 4 {
// `Result::Ok` can convert into `RespResult::Success` just using `into`
Ok(format!("welcome! {name} with size great then 4")).into()
}else{
// or just direct using `RespResult::ok`
RespResult::ok(format!("welcome! {name}"))
}
}
In general the RespResult::Success is always generate response with status code 200 OK and using serde_json
serialize the body into json. But sometimes we want to return an 304 Not Modified with empty body to tell the client
the resource does not change. To support above using a situation, comes out the ExtraFlag and ExtraFlags
extra flag have 4 different types can bring different effects on response
empty_body: this flag will stop RespResult perform serialize into response bodystatus: this flag will overwrite StatusCode of responseset-header: this flag will insert or append provide header into the response header mapremove-header: this flag will remove header from the response header mapdifferent extra flags can use + to combine effect or += to adding effect
extra flags is a set of extra flag
flag wrap provides a wrap to send the extra flag
when using the extra flag, you need changing the return type from RespResult<T, E> to RespResult<FlagWrap<T>, E>
the follow example change Status Code to 404 Not Found
use resp_result::{RespError, RespResult, FlagWrap, ExtraFlag};
use std::borrow::Cow;
use http::StatusCode;
pub struct PlainError(String);
impl RespError for PlainError{
fn log_message(&self) -> Cow<'_, str> {
Cow::Owned(format!("PlainError: {}", self.0))
}
type ExtraMessage = String;
fn extra_message(&self) -> Self::ExtraMessage {
self.0.clone()
}
}
/// this can be used as the handler return type
type PlainRResult<T> = RespResult<T, PlainError>;
pub async fn welcome_short_name(
name: String,
) -> PlainRResult<FlagWrap<String>>{
if name.len() >= 8{
RespResult::ok(
format!("welcome! {name} your name size is {}",name.len()))
// using `with_flags` to covert RespResult<T, E>
// to `RespResult<FlagWrap<T>, E>`
// using `()` for no extra flags
.with_flags(())
}else{
// using `flag_ok` directly construct a flag with the resp result
RespResult::flag_ok(
format!("Welcome! {name}"),
ExtraFlag::status(StatusCode::NOT_FOUND)
)
}
}
RespResult behaviorby default the RespResult will serialize the response body like that
{
"is-ok": true,
"error-message": "...",
"extra-msg": "...",
"body": null
}
the default behavior can be changed by using set_config to set global configuration
for example, by config, we can change the response body into following
{
"status": "fail",
"reterror": 10001,
"message": "something wrong",
"body": null
}
See the doc of ConfigTrait for more information
resp_result attribute macroThis macro is used on the function. It will convert the original Result<T,E> into the RespResult,
this makes writing handler more convenience.
Note: require
EinResultimplement theRespError
// the `rresult` is an alias of `resp_result`
// the function `test` now will return a `RespResult`
#[rresult]
fn test((a, b): (i32, i64), foo: String) -> Result<(), PlainError> {
println!("{a},{b},{foo}");
let a = foo.parse::<i32>()?;
println!("{a:?}");
Ok(())
}
RespError derive macroUsing this macro while implement RespError for the enum, usually using with thiserror
It now has 2 arg on each variant of enum
err_msg : the message return to the client, usually need to erase the sensitive message compare with log_message. if not provided it will using log_messageerr_code: the Http Status Code returned by this kind of error. If not provide, will be 500Here is an example
use std::num::ParseIntError;
use axum::extract::rejection::PathRejection;
use axum_resp_result::RespError;
#[derive(Debug, thiserror::Error, RespError)]
pub(super) enum PlainError {
#[error("Parse Request Path Error: {0}")]
#[resp_result(
err_msg = "Parse Request Path Error",
// the error_code can either the code number or the code name
err_code = 400
)]
Path(#[from] PathRejection),
#[error("Parse Int Error: {0}")]
#[resp_result(err_msg = "Invalid Input Integer", err_code = "Bad Request")]
ParseInt(#[from] ParseIntError),
}