Create a handler that can yield either a successful response or an error message? #2268
-
Is there a solution to create custom response handler by writing structure like this: pub enum CustomResponse<T> {
Body(T),
Error(String),
} And call it in api handler like this? #[derive(Debug, Deserialize)]
pub struct Data {
pub name: String,
}
pub async fn demo() -> CustomResponse<Data> {
// If error return
CustomResponse::Error("Error message".to_string());
// If success return
CustomResponse::Body(Data {
name: "Alex".to_string(),
})
} |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Oct 12, 2023
Replies: 1 comment
-
Yes. Did you read the docs here https://docs.rs/axum/latest/axum/response/index.html? There are lots of examples. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
kyborq
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes. Did you read the docs here https://docs.rs/axum/latest/axum/response/index.html? There are lots of examples.