{{! Copyright (c) Meta Platforms, Inc. and affiliates. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. }} /// Error definitions for `{{service:rust_name}}`. pub mod {{service:snake}} { {{#service:rust_exceptions}} pub trait As{{#rust_exception:type}}{{type:rust_name}}{{/rust_exception:type}} { fn as_{{#rust_exception:type}}{{type:rust_name_snake}}{{/rust_exception:type}}(&self) -> ::std::option::Option<&{{#rust_exception:type}}{{>lib/type}}{{/rust_exception:type}}>; } impl As{{#rust_exception:type}}{{type:rust_name}}{{/rust_exception:type}} for ::anyhow::Error { fn as_{{#rust_exception:type}}{{type:rust_name_snake}}{{/rust_exception:type}}(&self) -> ::std::option::Option<&{{#rust_exception:type}}{{>lib/type}}{{/rust_exception:type}}> { for cause in self.chain() { {{#rust_exception:functions}} {{#rust_exception_function:function}} if let ::std::option::Option::Some({{function:upcamel}}Error::{{#rust_exception_function:field}}{{field:rust_name}}{{/rust_exception_function:field}}(e)) = cause.downcast_ref::<{{function:upcamel}}Error>() { return ::std::option::Option::Some(e); } {{/rust_exception_function:function}} {{/rust_exception:functions}} } ::std::option::Option::None } } {{/service:rust_exceptions}} {{#service:rustFunctions}} {{^function:starts_interaction?}}{{^function:sink?}} {{#function:exceptions?}} /// Errors for {{function:rust_name}} (client side). #[derive(Debug)] pub enum {{function:upcamel}}Error { {{#function:exceptions}} {{field:rust_name}}({{#field:type}}{{>lib/type}}{{/field:type}}), {{/function:exceptions}} ApplicationException(::fbthrift::ApplicationException), ThriftError(::anyhow::Error), } /// Human-readable string representation of the Thrift client error. /// /// By default, this will not print the full cause chain. If you would like to print the underlying error /// cause, either use `format!("{:?}", anyhow::Error::from(client_err))` or print this using the /// alternate formatter `{:#}` instead of just `{}`. impl ::std::fmt::Display for {{function:upcamel}}Error { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::result::Result<(), ::std::fmt::Error> { match self { {{#function:exceptions}} Self::{{field:rust_name}}(inner) => { if f.alternate() { write!(f, "{{service:rust_name}}::{{function:rust_name}} failed with variant `{{field:rust_name}}`: {:#}", inner)?; } else { write!(f, "{{service:rust_name}}::{{function:rust_name}} failed with {{field:rust_name}}({{#field:type}}{{type:rust_name}}{{/field:type}})")?; } } {{/function:exceptions}} Self::ApplicationException(inner) => { write!(f, "{{service:rust_name}}::{{function:rust_name}} failed with ApplicationException")?; if f.alternate() { write!(f, ": {:#}", inner)?; } } Self::ThriftError(inner) => { write!(f, "{{service:rust_name}}::{{function:rust_name}} failed with ThriftError")?; if f.alternate() { write!(f, ": {:#}", inner)?; } } } ::std::result::Result::Ok(()) } } impl ::std::error::Error for {{function:upcamel}}Error { fn source(&self) -> ::std::option::Option<&(dyn ::std::error::Error + 'static)> { match self { {{#function:exceptions}} Self::{{field:rust_name}}(ref inner) => { ::std::option::Option::Some(inner) } {{/function:exceptions}} Self::ApplicationException(ref inner) => { ::std::option::Option::Some(inner) } Self::ThriftError(ref inner) => { ::std::option::Option::Some(inner.as_ref()) } } } } {{#function:uniqueExceptions}} impl ::std::convert::From<{{#field:type}}{{>lib/type}}{{/field:type}}> for {{function:upcamel}}Error { fn from(e: {{#field:type}}{{>lib/type}}{{/field:type}}) -> Self { Self::{{field:rust_name}}(e) } } impl As{{#field:type}}{{type:rust_name}}{{/field:type}} for {{function:upcamel}}Error { fn as_{{#field:type}}{{type:rust_name_snake}}{{/field:type}}(&self) -> ::std::option::Option<&{{#field:type}}{{>lib/type}}{{/field:type}}> { match self { Self::{{field:rust_name}}(inner) => ::std::option::Option::Some(inner), _ => ::std::option::Option::None, } } } {{/function:uniqueExceptions}} impl ::std::convert::From<::anyhow::Error> for {{function:upcamel}}Error { fn from(err: ::anyhow::Error) -> Self { Self::ThriftError(err) } } impl ::std::convert::From<::fbthrift::ApplicationException> for {{function:upcamel}}Error { fn from(ae: ::fbthrift::ApplicationException) -> Self { Self::ApplicationException(ae) } } impl ::std::convert::From<{{program:crate}}::services::{{service:snake}}::{{function:upcamel}}Exn> for {{function:upcamel}}Error { fn from(e: {{program:crate}}::services::{{service:snake}}::{{function:upcamel}}Exn) -> Self { match e { {{program:crate}}::services::{{service:snake}}::{{function:upcamel}}Exn::ApplicationException(aexn) => {{function:upcamel}}Error::ApplicationException(aexn), {{#function:exceptions}} {{program:crate}}::services::{{service:snake}}::{{function:upcamel}}Exn::{{field:rust_name}}(exn) => {{function:upcamel}}Error::{{field:rust_name}}(exn), {{/function:exceptions}} } } } impl ::std::convert::From<{{function:upcamel}}Error> for {{program:crate}}::services::{{service:snake}}::{{function:upcamel}}Exn { fn from(err: {{function:upcamel}}Error) -> Self { match err { {{#function:exceptions}} {{function:upcamel}}Error::{{field:rust_name}}(err) => {{program:crate}}::services::{{service:snake}}::{{function:upcamel}}Exn::{{field:rust_name}}(err), {{/function:exceptions}} {{function:upcamel}}Error::ApplicationException(aexn) => {{program:crate}}::services::{{service:snake}}::{{function:upcamel}}Exn::ApplicationException(aexn), {{function:upcamel}}Error::ThriftError(err) => {{program:crate}}::services::{{service:snake}}::{{function:upcamel}}Exn::ApplicationException(::fbthrift::ApplicationException { message: err.to_string(), type_: ::fbthrift::ApplicationExceptionErrorCode::InternalError, }), } } } {{/function:exceptions?}} {{^function:exceptions?}} pub type {{function:upcamel}}Error = ::fbthrift::NonthrowingFunctionError; {{/function:exceptions?}} pub(crate) enum {{function:upcamel}}Reader {} impl ::fbthrift::help::DeserializeExn for {{function:upcamel}}Reader { type Success = {{! }}{{^function:stream?}}{{>lib/server_ok_type}}{{/function:stream?}}{{! }}{{#function:stream?}}{{! }}{{#function:stream_has_first_response?}}{{! }}{{#function:return_type}}{{#function:stream_first_response_type}}{{>lib/type}}{{/function:stream_first_response_type}}{{/function:return_type}}{{! }}{{/function:stream_has_first_response?}}{{! }}{{^function:stream_has_first_response?}}(){{/function:stream_has_first_response?}}{{! }}{{/function:stream?}}{{! }}; type Error = {{function:upcamel}}Error; fn read_result
(p: &mut P) -> ::anyhow::Result<::std::result::Result (p: &mut P) -> ::anyhow::Result<::std::result::Result