fbthrift_git/exceptions.rs
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Helpers for generated code using exceptions
18
19use crate::thrift_protocol::MessageType;
20
21/// This trait should be implemented for each individual exception type. It will typically be generated.
22pub trait ExceptionInfo {
23 /// Exception name
24 fn exn_name(&self) -> &'static str {
25 std::any::type_name::<Self>()
26 }
27
28 // Exception value
29 fn exn_value(&self) -> String;
30
31 /// Is a declared exception
32 fn exn_is_declared(&self) -> bool;
33}
34
35/// An extension of ExceptionInfo that also includes successful results.
36/// This is implemented on generated *Exn types.
37pub trait ResultInfo: ExceptionInfo {
38 fn result_type(&self) -> ResultType;
39}
40
41/// Classify a result from a specific method call.
42#[derive(Debug, Clone, Copy, Eq, PartialEq)]
43pub enum ResultType {
44 /// A successful return
45 Return,
46 /// A declared exception
47 Error,
48 /// Some other exception (eg ApplicationException)
49 Exception,
50}
51
52impl ResultType {
53 pub fn message_type(&self) -> MessageType {
54 match self {
55 ResultType::Return | ResultType::Error => MessageType::Reply,
56 ResultType::Exception => MessageType::Exception,
57 }
58 }
59}