Skip to content

Commit

Permalink
replace ApplicationError with AnyhowError in generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ggiggle committed Feb 2, 2024
1 parent 7728f2e commit 0b6d9c4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions volo-build/src/thrift_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
format! {
r#"match self.inner.{name}({args}).await {{
Ok(resp) => {method_result_path}::Ok(resp),
Err(err) => return Err(::volo_thrift::error::ApplicationError::new(::volo_thrift::error::ApplicationErrorKind::INTERNAL_ERROR, err.to_string())),
Err(err) => return Err(::volo_thrift::Error::Anyhow(anyhow::anyhow!(err))),
}}"#
}
}
Expand Down Expand Up @@ -586,7 +586,7 @@ impl pilota_build::CodegenBackend for VoloThriftBackend {
impl<T> ::volo::service::Service<::volo_thrift::context::ServerContext, {req_recv_name}> for {server_name}<T> where T: {service_name} + Send + Sync + 'static {{
type Response = {res_send_name};
type Error = ::volo_thrift::error::ApplicationError;
type Error = ::volo_thrift::Error;
async fn call<'s, 'cx>(&'s self, _cx: &'cx mut ::volo_thrift::context::ServerContext, req: {req_recv_name}) -> ::std::result::Result<Self::Response, Self::Error> {{
match req {{
Expand Down
14 changes: 12 additions & 2 deletions volo-thrift/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub enum Error {
/// Basic error types, will be converted to `ApplicationError::INTERNAL_ERROR`
/// when encoding.
Basic(BasicError),
Anyhow(AnyhowError),
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -73,6 +74,11 @@ impl Error {
}
Error::Application(e) => e.message.push_str(msg),
Error::Basic(e) => e.message.push_str(msg),
Error::Anyhow(e) => {
let mut err_str = e.to_string();
err_str.push_str(msg);
*self = Self::Anyhow(anyhow::anyhow!(err_str));
}
}
}
}
Expand Down Expand Up @@ -165,13 +171,13 @@ impl Retryable for Error {

impl From<AnyhowError> for Error {
fn from(err: AnyhowError) -> Self {
new_application_error(ApplicationErrorKind::UNKNOWN, err.to_string())
Error::Anyhow(err)
}
}

impl From<Box<dyn std::error::Error + Send + Sync>> for Error {
fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
new_application_error(ApplicationErrorKind::UNKNOWN, err.to_string())
Error::Anyhow(anyhow::anyhow!(err))
}
}

Expand Down Expand Up @@ -540,6 +546,10 @@ impl<T> From<Error> for ResponseError<T> {
Error::Protocol(e) => ResponseError::Protocol(e),
Error::Application(e) => ResponseError::Application(e),
Error::Basic(e) => ResponseError::Basic(e),
Error::Anyhow(e) => ResponseError::Application(ApplicationError::new(
ApplicationErrorKind::INTERNAL_ERROR,
e.to_string(),
)),
}
}
}
6 changes: 6 additions & 0 deletions volo-thrift/src/message_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ where
);
e.encode(protocol)?;
}
crate::Error::Anyhow(e) => {
protocol.write_message_begin(&ident)?;
let e =
ApplicationError::new(ApplicationErrorKind::INTERNAL_ERROR, e.to_string());
e.encode(protocol)?;
}
},
}
protocol.write_message_end()?;
Expand Down

0 comments on commit 0b6d9c4

Please sign in to comment.