lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

error.rs (6296B)


      1 use super::{Admission, SignerRejection};
      2 use wasm_bindgen::prelude::*;
      3 
      4 /// Pure command-parser failures retained across the native boundary.
      5 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
      6 pub enum InputFailure {
      7     UnsupportedCommand,
      8     UnsupportedProfile,
      9     LimitExceeded,
     10 }
     11 
     12 /// Input-free failures; native details never become dependency/debug prose.
     13 #[derive(Clone, Debug)]
     14 pub enum Error {
     15     InvalidInput {
     16         field: String,
     17     },
     18     CommandInput {
     19         field: String,
     20         kind: InputFailure,
     21     },
     22     Operation {
     23         code: String,
     24         retry: String,
     25         operation_id: Option<String>,
     26         admission: Admission,
     27         signer_rejection: Option<Box<SignerRejection>>,
     28     },
     29     UnsupportedState {
     30         axis: String,
     31         operation_id: Option<String>,
     32         signer_rejection: Option<Box<SignerRejection>>,
     33     },
     34 }
     35 const _: () = assert!(core::mem::size_of::<Error>() <= 128);
     36 impl Error {
     37     pub(crate) fn command(
     38         field: &'static str,
     39         error: radroots_protocol::command::CommandError,
     40     ) -> Self {
     41         let kind = match error {
     42             radroots_protocol::command::CommandError::InvalidInput => return Self::input(field),
     43             radroots_protocol::command::CommandError::UnsupportedCommand => {
     44                 InputFailure::UnsupportedCommand
     45             }
     46             radroots_protocol::command::CommandError::UnsupportedProfile => {
     47                 InputFailure::UnsupportedProfile
     48             }
     49             radroots_protocol::command::CommandError::LimitExceeded => InputFailure::LimitExceeded,
     50             _ => {
     51                 return Self::UnsupportedState {
     52                     axis: "command_input".to_owned(),
     53                     operation_id: None,
     54                     signer_rejection: None,
     55                 };
     56             }
     57         };
     58         Self::CommandInput {
     59             field: field.to_owned(),
     60             kind,
     61         }
     62     }
     63     pub(crate) fn input(field: &'static str) -> Self {
     64         Self::InvalidInput {
     65             field: field.to_owned(),
     66         }
     67     }
     68     pub(crate) fn with_operation(self, id: String) -> Self {
     69         match self {
     70             Self::UnsupportedState {
     71                 axis,
     72                 signer_rejection,
     73                 ..
     74             } => Self::UnsupportedState {
     75                 axis,
     76                 operation_id: Some(id),
     77                 signer_rejection,
     78             },
     79             value => value,
     80         }
     81     }
     82     fn operation(
     83         value: radroots_execution::Error,
     84         signer_rejection: Option<Box<SignerRejection>>,
     85     ) -> Self {
     86         let operation_id = value.context().operation_id().map(|v| v.to_string());
     87         match Admission::try_from(value.context().admission()) {
     88             Ok(admission) => Self::Operation {
     89                 code: value.code().as_str().to_owned(),
     90                 retry: value.retry().as_str().to_owned(),
     91                 operation_id,
     92                 admission,
     93                 signer_rejection,
     94             },
     95             Err(Self::UnsupportedState { axis, .. }) => Self::UnsupportedState {
     96                 axis,
     97                 operation_id,
     98                 signer_rejection,
     99             },
    100             Err(_) => Self::UnsupportedState {
    101                 axis: "admission".into(),
    102                 operation_id,
    103                 signer_rejection,
    104             },
    105         }
    106     }
    107 }
    108 impl From<radroots_execution::Error> for Error {
    109     fn from(value: radroots_execution::Error) -> Self {
    110         Self::operation(value, None)
    111     }
    112 }
    113 impl From<radroots_execution::ExecutionFailure> for Error {
    114     fn from(value: radroots_execution::ExecutionFailure) -> Self {
    115         Self::operation(
    116             value.error(),
    117             value.signer_rejection().map(|v| Box::new(v.into())),
    118         )
    119     }
    120 }
    121 
    122 /// Input-free exception value. No private input, provider cause or path crosses.
    123 #[derive(Clone)]
    124 #[wasm_bindgen]
    125 pub struct Failure {
    126     #[wasm_bindgen(readonly, getter_with_clone)]
    127     pub code: String,
    128     #[wasm_bindgen(readonly, getter_with_clone)]
    129     pub retry: Option<String>,
    130     #[wasm_bindgen(readonly, getter_with_clone)]
    131     pub operation_id: Option<String>,
    132     #[wasm_bindgen(readonly)]
    133     pub admission: Option<Admission>,
    134     #[wasm_bindgen(readonly, getter_with_clone)]
    135     pub field: Option<String>,
    136     #[wasm_bindgen(readonly, getter_with_clone)]
    137     pub signer_rejection: Option<SignerRejection>,
    138 }
    139 impl From<Error> for Failure {
    140     fn from(value: Error) -> Self {
    141         match value {
    142             Error::InvalidInput { field } => Self {
    143                 code: "InvalidInput".into(),
    144                 retry: None,
    145                 operation_id: None,
    146                 admission: None,
    147                 field: Some(field),
    148                 signer_rejection: None,
    149             },
    150             Error::CommandInput { field, kind } => Self {
    151                 code: match kind {
    152                     InputFailure::UnsupportedCommand => "UnsupportedCommand",
    153                     InputFailure::UnsupportedProfile => "UnsupportedProfile",
    154                     InputFailure::LimitExceeded => "LimitExceeded",
    155                 }
    156                 .into(),
    157                 retry: None,
    158                 operation_id: None,
    159                 admission: None,
    160                 field: Some(field),
    161                 signer_rejection: None,
    162             },
    163             Error::Operation {
    164                 code,
    165                 retry,
    166                 operation_id,
    167                 admission,
    168                 signer_rejection,
    169             } => Self {
    170                 code,
    171                 retry: Some(retry),
    172                 operation_id,
    173                 admission: Some(admission),
    174                 field: None,
    175                 signer_rejection: signer_rejection.map(|v| *v),
    176             },
    177             Error::UnsupportedState {
    178                 axis,
    179                 operation_id,
    180                 signer_rejection,
    181             } => Self {
    182                 code: "UnsupportedState".into(),
    183                 retry: None,
    184                 operation_id,
    185                 admission: None,
    186                 field: Some(axis),
    187                 signer_rejection: signer_rejection.map(|v| *v),
    188             },
    189         }
    190     }
    191 }
    192 impl From<Error> for JsValue {
    193     fn from(value: Error) -> Self {
    194         Failure::from(value).into()
    195     }
    196 }