-
I want to add i18n capability for my application, I unify all errors to my server error enum which can be mapped to an error code. pub async fn register(
Extension(ref db): Extension<DatabaseConnection>,
Json(to_be_created): Json<CreateUser>,
) -> Result<CreateUserRsp, ServerError> {
xxx
} However, when I tried to log response in middleware ,I found all results returned by my handler will transform to Ok(Response(UnsyncBodyxxxx)) (Ok is 200,Err is 500 or others) let accept_language = req
.headers()
.get(header::ACCEPT_LANGUAGE)
.map(HeaderValue::to_str)
.map(|result| result.ok())
.flatten()
.map(ToString::to_string);
let res_future = self.inner.call(req);
Box::pin(res_future.inspect(|res| {
dbg!(&res);
})) So if I want to get my origin SeverError and translate my error code by accept-language header, I will have to try to deserialize the HTTP body to ServerError. is there a lifetime I can use to get my origin response before it becomes a response body? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
finally, I struggled with nested futures and had to write like below... Box::pin(async move {
let result = res_future.await;
let translation_config = TranslationConfig {
accept_language,
..Default::default()
};
match result {
Ok(response) if !response.status().is_success() => {
let body = response.into_body();
let body = hyper::body::to_bytes(body)
.await
.ok()
.and_then(|bytes| serde_json::from_slice(&bytes).ok())
.unwrap_or(TmpError::from(ServerError::InternalServerError))
.translate(translation_config);
Ok(body.into_response())
}
Ok(response) => Ok(response),
Err(err) => Err(err),
}
}) |
Beta Was this translation helpful? Give feedback.
finally, I struggled with nested futures and had to write like below...
body => bytes => str => struct => body .a little hard for me.