Skip to main content

rialo_types/
error.rs

1// Copyright (c) Subzero Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use borsh::{BorshDeserialize, BorshSerialize};
5use serde::{Deserialize, Serialize};
6use thiserror::Error;
7
8use crate::HeadersError;
9
10/// Represents errors that can occur during REX processing.
11#[derive(
12    Clone, Debug, Eq, Error, PartialEq, Serialize, Deserialize, BorshSerialize, BorshDeserialize,
13)]
14pub enum RexError {
15    #[error("Missing required parameter: {param}")]
16    MissingParameter { param: String },
17
18    #[error("Invalid parameter '{param}': {reason}")]
19    InvalidParameter { param: String, reason: String },
20
21    #[error("Failed to decode {encoding} for parameter '{param}': {reason}")]
22    DecodeError {
23        param: String,
24        encoding: String,
25        reason: String,
26    },
27
28    #[error("Invalid URL: {reason}")]
29    InvalidUrl { reason: String },
30
31    #[error("URL validation failed: {reason}")]
32    UrlValidationError { reason: String },
33
34    #[error("HTTP request failed: {message}{}", reason.as_ref().map(|r| format!(", {}", r)).unwrap_or_default()
35    )]
36    HttpRequestFailed {
37        message: String,
38        reason: Option<String>,
39    },
40
41    #[error("HTTP {status} error: {reason}")]
42    HttpStatusError { status: u16, reason: String },
43
44    #[error("Response size exceeds maximum of {max_size} bytes")]
45    ResponseTooLarge { max_size: usize },
46
47    #[error("Request body size of {size} bytes exceeds maximum of {max_size} bytes")]
48    RequestBodyTooLarge { size: usize, max_size: usize },
49
50    #[error("Filter error: {reason}")]
51    FilterError { reason: String },
52
53    #[error("Timeout after {duration_ms}ms")]
54    Timeout { duration_ms: u64 },
55
56    #[error("All {attempts} retry attempts failed: {reason}")]
57    RetriesExhausted { attempts: u8, reason: String },
58
59    #[error("JSON parsing error: {reason}")]
60    JsonError { reason: String },
61
62    #[error("No valid data sources available")]
63    NoDataSources,
64
65    #[error("Failed to decrypt secret payload: {reason}")]
66    SecretDecryptionFailed { reason: String },
67
68    #[error("Invalid secret payload format: {reason}")]
69    InvalidSecretPayload { reason: String },
70
71    #[error("Failed to generate secret key: {reason}")]
72    SecretKeyGenerationFailed { reason: String },
73
74    #[error("Failed to encrypt secret: {reason}")]
75    SecretEncryptionFailed { reason: String },
76
77    #[error("Provided REX output size is too small: {provided}")]
78    RexOutputSizeTooSmall { provided: usize },
79
80    #[error("Invalid REX duty input, err: {0}")]
81    InvalidRexDutyInput(String),
82
83    #[error("Connection error: {reason}")]
84    ConnectionError { reason: String },
85
86    #[error("WebSocket error: {message}")]
87    WebSocketError { message: String },
88
89    // DKG-specific errors
90    #[error("DKG input validation failed: {reason}")]
91    DkgValidationError { reason: String },
92
93    #[error("DKG cryptographic operation failed ({operation}): {reason}")]
94    DkgCryptoError { operation: String, reason: String },
95
96    #[error("DKG handler not implemented: {handler}")]
97    DkgNotImplemented { handler: String },
98
99    #[error("DKG genesis failed: {reason}")]
100    DkgGenesisError { reason: String },
101
102    #[error("DKG share installation failed: {reason}")]
103    DkgInstallShareError { reason: String },
104
105    #[error("DKG share decryption (HPKE) failed for dealer {dealer_index}: {reason}")]
106    DkgDecryptionError { dealer_index: u16, reason: String },
107
108    #[error("DKG share verification failed for dealer {dealer_index}")]
109    DkgVerificationError { dealer_index: u16 },
110
111    #[error("DKG partial decryption failed: {reason}")]
112    DkgPartialDecryptError { reason: String },
113
114    #[error("DKG combine error: {reason}")]
115    DkgCombineError { reason: String },
116
117    #[error("DKG AEAD decryption failed: invalid ciphertext or authentication tag")]
118    DkgAeadDecryptFailed,
119
120    #[error("DKG key derivation (HKDF) failed")]
121    DkgHkdfExpandFailed,
122
123    #[error("DKG invalid key length")]
124    DkgInvalidKeyLength,
125    #[error("WASM execution error: {reason}")]
126    WasmExecutionError { reason: String },
127}
128
129impl From<HeadersError> for RexError {
130    fn from(err: HeadersError) -> Self {
131        match err {
132            HeadersError::DecodeError {
133                param,
134                encoding,
135                reason,
136            } => Self::DecodeError {
137                param,
138                encoding,
139                reason,
140            },
141            HeadersError::JsonError { reason } => Self::JsonError { reason },
142        }
143    }
144}