Skip to main content

sp1_cuda/
error.rs

1use std::{error::Error, fmt, io::Error as IoError};
2
3pub struct CudaClientError {
4    variant: CudaClientErrorVariant,
5    ctx: Option<String>,
6}
7
8impl fmt::Display for CudaClientError {
9    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10        write!(f, "CudaClientError: {}", self.variant)
11    }
12}
13
14impl fmt::Debug for CudaClientError {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        writeln!(f, "CudaClientError: {:?}", self.variant)?;
17        if let Some(ctx) = &self.ctx {
18            writeln!(f, "Context: {ctx:?}")?;
19        }
20        Ok(())
21    }
22}
23
24impl Error for CudaClientError {}
25
26impl CudaClientError {
27    pub const fn new(variant: CudaClientErrorVariant) -> Self {
28        Self { variant, ctx: None }
29    }
30
31    pub fn new_with_ctx(variant: CudaClientErrorVariant, ctx: impl Into<String>) -> Self {
32        Self { variant, ctx: Some(ctx.into()) }
33    }
34
35    pub fn context(&mut self, ctx: impl Into<String>) {
36        self.ctx = Some(ctx.into());
37    }
38}
39
40#[allow(non_snake_case)]
41impl CudaClientError {
42    pub const fn Connect(err: IoError) -> Self {
43        Self::new(CudaClientErrorVariant::Connect(err))
44    }
45
46    pub fn new_connect(err: IoError, ctx: &str) -> Self {
47        Self::new_with_ctx(CudaClientErrorVariant::Connect(err), ctx)
48    }
49
50    pub const fn Serialize(err: bincode::Error) -> Self {
51        Self::new(CudaClientErrorVariant::Serialize(err))
52    }
53
54    pub fn new_serialize(err: bincode::Error, ctx: &str) -> Self {
55        Self::new_with_ctx(CudaClientErrorVariant::Serialize(err), ctx)
56    }
57
58    pub const fn Deserialize(err: bincode::Error) -> Self {
59        Self::new(CudaClientErrorVariant::Deserialize(err))
60    }
61
62    pub fn new_deserialize(err: bincode::Error, ctx: &str) -> Self {
63        Self::new_with_ctx(CudaClientErrorVariant::Deserialize(err), ctx)
64    }
65
66    pub const fn Write(err: IoError) -> Self {
67        Self::new(CudaClientErrorVariant::Write(err))
68    }
69
70    pub fn new_write(err: IoError, ctx: &str) -> Self {
71        Self::new_with_ctx(CudaClientErrorVariant::Write(err), ctx)
72    }
73
74    pub fn Read(err: IoError) -> Self {
75        Self::new(CudaClientErrorVariant::Read(err))
76    }
77
78    pub fn new_read(err: IoError, ctx: &str) -> Self {
79        Self::new_with_ctx(CudaClientErrorVariant::Read(err), ctx)
80    }
81
82    pub const fn ServerError(err: String) -> Self {
83        Self::new(CudaClientErrorVariant::ServerError(err))
84    }
85
86    pub fn new_server_error(err: String, ctx: &str) -> Self {
87        Self::new_with_ctx(CudaClientErrorVariant::ServerError(err), ctx)
88    }
89
90    pub const fn UnexpectedResponse(response: &'static str) -> Self {
91        Self::new(CudaClientErrorVariant::UnexpectedResponse(response))
92    }
93
94    pub fn new_unexpected_response(response: &'static str, ctx: &str) -> Self {
95        Self::new_with_ctx(CudaClientErrorVariant::UnexpectedResponse(response), ctx)
96    }
97
98    pub const fn ProverError(err: String) -> Self {
99        Self::new(CudaClientErrorVariant::ProverError(err))
100    }
101
102    pub fn new_prover_error(err: String, ctx: &str) -> Self {
103        Self::new_with_ctx(CudaClientErrorVariant::ProverError(err), ctx)
104    }
105
106    pub const fn Download(err: reqwest::Error) -> Self {
107        Self::new(CudaClientErrorVariant::Download(err))
108    }
109
110    pub fn new_download(err: reqwest::Error, ctx: &str) -> Self {
111        Self::new_with_ctx(CudaClientErrorVariant::Download(err), ctx)
112    }
113
114    pub const fn DownloadIO(err: IoError) -> Self {
115        Self::new(CudaClientErrorVariant::DownloadIO(err))
116    }
117
118    pub fn new_download_io(err: IoError, ctx: &str) -> Self {
119        Self::new_with_ctx(CudaClientErrorVariant::DownloadIO(err), ctx)
120    }
121
122    pub const fn Unexpected(err: String) -> Self {
123        Self::new(CudaClientErrorVariant::Unexpected(err))
124    }
125
126    pub fn new_unexpected(err: String, ctx: &str) -> Self {
127        Self::new_with_ctx(CudaClientErrorVariant::Unexpected(err), ctx)
128    }
129}
130
131#[derive(Debug, thiserror::Error)]
132pub enum CudaClientErrorVariant {
133    #[error("Failed to connect to the server: {0:?}")]
134    Connect(IoError),
135
136    #[error("Failed to serialize the request: {0:?}")]
137    Serialize(bincode::Error),
138
139    #[error("Failed to deserialize the response: {0:?}")]
140    Deserialize(bincode::Error),
141
142    #[error("Failed to write the request: {0:?}")]
143    Write(IoError),
144
145    #[error("Failed to read the response: {0:?}")]
146    Read(IoError),
147
148    #[error("The server returned an internal error \n {0}")]
149    ServerError(String),
150
151    #[error("The server returned an unexpected response: {0:?}")]
152    UnexpectedResponse(&'static str),
153
154    #[error("The server returned a prover error: {0:?}")]
155    ProverError(String),
156
157    #[error("Failed to download the server: {0:?}")]
158    Download(#[from] reqwest::Error),
159
160    #[error("Failed to download the server: {0:?}")]
161    DownloadIO(std::io::Error),
162
163    #[error("Unexpected error: {0:?}")]
164    Unexpected(String),
165}