open_feature/evaluation/
error.rs

1// ============================================================
2//  EvaluationError
3// ============================================================
4
5use std::error::Error as StdError;
6use std::fmt::{Display, Formatter};
7use typed_builder::TypedBuilder;
8
9/// Struct representing error
10#[derive(Clone, Eq, PartialEq, TypedBuilder, Debug)]
11pub struct EvaluationError {
12    /// The error code of abnormal evaluation.
13    pub code: EvaluationErrorCode,
14
15    /// The custom error message returned by the provider.
16    #[builder(default, setter(strip_option, into))]
17    pub message: Option<String>,
18}
19
20// ============================================================
21//  EvaluationErrorCode
22// ============================================================
23
24/// An enumerated error code represented idiomatically in the implementation language.
25#[derive(Clone, Eq, PartialEq, Debug)]
26pub enum EvaluationErrorCode {
27    /// The value was resolved before the provider was initialized.
28    ProviderNotReady,
29
30    /// The flag could not be found.
31    FlagNotFound,
32
33    /// An error was encountered parsing data, such as a flag configuration.
34    ParseError,
35
36    /// The type of the flag value does not match the expected type.
37    TypeMismatch,
38
39    /// The provider requires a targeting key and one was not provided in the evaluation context.
40    TargetingKeyMissing,
41
42    /// The evaluation context does not meet provider requirements.
43    InvalidContext,
44
45    /// The error was for a reason not enumerated above.
46    General(String),
47}
48
49impl Display for EvaluationErrorCode {
50    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
51        let code = match self {
52            Self::ProviderNotReady => "PROVIDER_NOT_READY",
53            Self::FlagNotFound => "FLAG_NOT_FOUND",
54            Self::ParseError => "PARSE_ERROR",
55            Self::TypeMismatch => "TYPE_MISMATCH",
56            Self::TargetingKeyMissing => "TARGETING_KEY_MISSING",
57            Self::InvalidContext => "INVALID_CONTEXT",
58            Self::General(message) => message,
59        };
60        write!(f, "{code}")
61    }
62}
63
64impl StdError for EvaluationErrorCode {}