privy_rs/
errors.rs

1#![allow(missing_docs)]
2
3use thiserror::Error;
4
5pub use crate::generated::{Error as PrivyApiError, types::error::ConversionError};
6
7/// Errors that can occur during `PrivyClient` initialization.
8#[derive(Error, Debug)]
9pub enum PrivyCreateError {
10    #[error("Invalid header value: {0}")]
11    InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
12    #[error("Unable to create client: {0}")]
13    Client(#[from] reqwest::Error),
14    #[error("Invalid app id")]
15    InvalidAppId,
16    #[error("Invalid app secret")]
17    InvalidAppSecret,
18}
19
20/// The primary error type for the Privy SDK.
21///
22/// This enum consolidates all possible failures that can occur during client setup,
23/// API interaction, or cryptographic operations into a single, easy-to-handle type.
24#[derive(Error, Debug)]
25pub enum PrivySignedApiError {
26    /// An error returned by the Privy API (e.g., 4xx or 5xx HTTP status codes).
27    /// Contains the raw response for further inspection.
28    #[error("API request failed")]
29    Api(#[from] PrivyApiError),
30
31    /// An error occurred during the signing process.
32    #[error("Signature generation failed: {0}")]
33    SignatureGeneration(#[from] SignatureGenerationError),
34}
35
36/// Errors that can appear during wallet export.
37#[derive(Error, Debug)]
38pub enum PrivyExportError {
39    /// An error returned by the Privy API (e.g., 4xx or 5xx HTTP status codes).
40    /// Contains the raw response for further inspection.
41    #[error("API request failed")]
42    Api(#[from] PrivyApiError),
43
44    /// An error occurred during the signing process.
45    #[error("Signature generation failed: {0}")]
46    SignatureGeneration(#[from] SignatureGenerationError),
47
48    /// An error occurred during the decryption process.
49    #[error("Unable to decrypt key: {0}")]
50    Key(#[from] KeyError),
51}
52
53/// Errors related to cryptographic keys and operations.
54#[derive(Error, Debug)]
55pub enum CryptoError {
56    /// A failure occurred during the signing process.
57    #[error("Signing failed: {0}")]
58    Signing(#[from] SigningError),
59
60    /// A failure occurred while parsing, loading, or exchanging a key.
61    #[error("Key handling failed: {0}")]
62    Key(#[from] KeyError),
63}
64
65/// Errors related to handling cryptographic keys.
66#[derive(Error, Debug)]
67pub enum KeyError {
68    /// Failed to read a key from a file or other I/O source.
69    #[error("Key I/O error: {0}")]
70    Io(#[from] std::io::Error),
71
72    /// The key data is malformed (e.g., invalid PEM, DER, or Base64 format).
73    #[error("Invalid key format: {0}")]
74    InvalidFormat(String),
75
76    /// Failed to decrypt an HPKE-encrypted payload.
77    #[error("HPKE decryption failed: {0}")]
78    HpkeDecryption(#[from] hpke::HpkeError),
79
80    /// An unknown error occurred.
81    #[error(transparent)]
82    Other(Box<dyn std::error::Error + Send + Sync>),
83}
84
85/// Errors that occur specifically during a digital signature operation.
86#[derive(Error, Debug)]
87pub enum SigningError {
88    /// The key required for signing could not be obtained or is invalid.
89    #[error("Invalid key for signing: {0}")]
90    Key(#[from] KeyError),
91
92    /// The underlying cryptographic library failed to produce a signature.
93    #[error("Signature creation failed: {0}")]
94    Signature(#[from] p256::ecdsa::Error),
95
96    /// An unknown error occurred.
97    #[error(transparent)]
98    Other(Box<dyn std::error::Error + Send + Sync>),
99}
100
101/// Errors from the authorization signature generation process. This can
102/// very rarely occur from serialization (either the request could not
103/// be serialized or the serialized data can not be converted to base64),
104/// or (more likely) there was an error when undergoing the signing process.
105#[derive(Debug, Error)]
106pub enum SignatureGenerationError {
107    #[error("Unable to serialize request for signing: {0}")]
108    Serialization(#[from] serde_json::Error),
109    #[error("Error when signing request: {0}")]
110    Signing(#[from] SigningError),
111}