1use crate::prelude::rings_core;
3
4pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12#[repr(u32)]
13pub enum Error {
14 #[error("Connect remote rpc server failed: {0}.")]
15 RemoteRpcError(String) = 100,
16 #[error("Unknown rpc error.")]
17 UnknownRpcError = 101,
18 #[error("Internal rpc services error: {0}.")]
19 InternalRpcError(#[from] jsonrpc_core::Error) = 102,
20 #[error("Uuid error: {0}")]
21 UuidError(#[from] uuid::Error) = 103,
22 #[error("Connection not found.")]
23 ConnectionNotFound = 203,
24 #[error("Create connection error: {0}.")]
25 NewConnectionError(rings_core::error::Error) = 204,
26 #[error("Close connection error: {0}.")]
27 CloseConnectionError(rings_core::error::Error) = 205,
28 #[error("Invalid connection id.")]
29 InvalidConnectionId = 206,
30 #[error("Create offer info failed: {0}.")]
31 CreateOffer(rings_core::error::Error) = 207,
32 #[error("Answer offer info failed: {0}.")]
33 AnswerOffer(rings_core::error::Error) = 208,
34 #[error("Accept answer info failed: {0}.")]
35 AcceptAnswer(rings_core::error::Error) = 209,
36 #[error("Decode error.")]
37 DecodeError = 300,
38 #[error("Encode error.")]
39 EncodeError = 301,
40 #[error("WASM compile error: {0}")]
41 WasmCompileError(String) = 400,
42 #[error("BackendMessage RwLock Error")]
43 WasmBackendMessageRwLockError = 401,
44 #[error("WASM instantiation error.")]
45 WasmInstantiationError = 402,
46 #[error("WASM export error.")]
47 WasmExportError = 403,
48 #[error("WASM runtime error: {0}")]
49 WasmRuntimeError(String) = 404,
50 #[error("WASM global memory mutex error.")]
51 WasmGlobalMemoryLockError = 405,
52 #[error("WASM failed to load file.")]
53 WasmFailedToLoadFile = 406,
54 #[error("Invalid did: {0}")]
55 InvalidDid(String) = 500,
56 #[error("Invalid method.")]
57 InvalidMethod = 501,
58 #[error("Internal error: {0}.")]
59 InternalError(rings_core::error::Error) = 502,
60 #[error("No Permission")]
61 NoPermission = 504,
62 #[error("Connect error, {0}")]
63 ConnectError(rings_core::error::Error) = 600,
64 #[error("Send message error: {0}")]
65 SendMessage(rings_core::error::Error) = 601,
66 #[error("entry action error: {0}")]
67 EntryError(rings_core::error::Error) = 603,
68 #[error("service register action error: {0}")]
69 ServiceRegisterError(rings_core::error::Error) = 604,
70 #[error("JsError: {0}")]
71 JsError(String) = 700,
72 #[error("Invalid message")]
73 InvalidMessage = 800,
74 #[error("Invalid http request: {0}")]
75 HttpRequestError(String) = 801,
76 #[error("Invalid data")]
77 InvalidData = 802,
78 #[error("Invalid service")]
79 InvalidService = 803,
80 #[error("Invalid address")]
81 InvalidAddress = 804,
82 #[error("Invalid auth data")]
83 InvalidAuthData = 805,
84 #[error("invalid headers")]
85 InvalidHeaders = 806,
86 #[error("Storage Error: {0}")]
87 Storage(rings_core::error::Error) = 807,
88 #[error("Swarm Error: {0}")]
89 Swarm(rings_core::error::Error) = 808,
90 #[error("Invalid logging level: {0}")]
91 InvalidLoggingLevel(String) = 809,
92 #[error("Create File Error: {0}")]
93 CreateFileError(String) = 900,
94 #[error("Open File Error: {0}")]
95 OpenFileError(String) = 901,
96 #[error("Acquire lock failed")]
97 Lock = 902,
98 #[error("Cannot find home directory")]
99 HomeDirError = 903,
100 #[error("Cannot find parent directory")]
101 ParentDirError = 904,
102 #[error("Serde json error: {0}")]
103 SerdeJsonError(#[from] serde_json::Error) = 1000,
104 #[error("Serde yaml error: {0}")]
105 SerdeYamlError(#[from] serde_yaml::Error) = 1001,
106 #[error("verify error: {0}")]
107 VerifyError(String) = 1002,
108 #[error("Core error: {0}")]
109 CoreError(#[from] rings_core::error::Error) = 1102,
110 #[error("External singer error: {0}")]
111 ExternalError(String) = 1202,
112 #[error("An error indicating that an interior nul byte was found: {0}")]
113 FFINulError(#[from] std::ffi::NulError) = 1203,
114 #[error("Failed to convert CStr to String: {0}")]
115 FFICStrError(#[from] std::str::Utf8Error) = 1204,
116 #[error("An error indicating that a ptr is null")]
117 FFINulPtrError = 1205,
118 #[error("Failed to convert bytes to String: {0}")]
119 FFIFromUtf8Error(#[from] std::string::FromUtf8Error) = 1206,
120 #[cfg(feature = "snark")]
121 #[error("Snark error: {0}")]
122 RingsSNARKError(#[from] rings_snark::error::Error) = 1400,
123 #[error("Snark curve not match")]
124 SNARKCurveNotMatch() = 1401,
125 #[error("Snark handle message error: {0}")]
126 SNARKHandleMessage(String) = 1402,
127 #[error("Wrong field, should be {0}")]
128 SNARKWrongField(String) = 1403,
129 #[cfg(feature = "browser")]
130 #[error("range error when covering js_sys::BigInt to PrimeField: {0}")]
131 SNARKFFRangeError(String) = 1404,
132 #[cfg(feature = "browser")]
133 #[error("Failed to load bigint to repr string, it's empty")]
134 SNARKBigIntValueEmpty() = 1405,
135 #[error("Failed to load string to PrimeField")]
136 FailedToLoadFF() = 1406,
137 #[error("Extend Backend Error {0}")]
138 BackendError(String) = 1501,
139 #[error("Extension error: {0}")]
140 ExtensionError(String) = 1502,
141}
142
143impl Error {
144 fn discriminant(&self) -> u32 {
145 unsafe { *<*const _>::from(self).cast::<u32>() }
155 }
156
157 pub fn code(&self) -> u32 {
158 self.discriminant()
159 }
160}
161
162impl From<Error> for jsonrpc_core::Error {
163 fn from(e: Error) -> Self {
164 Self {
165 code: jsonrpc_core::ErrorCode::ServerError(e.code().into()),
166 message: e.to_string(),
167 data: None,
168 }
169 }
170}
171
172impl From<rings_rpc::error::Error> for Error {
173 fn from(e: rings_rpc::error::Error) -> Self {
174 match e {
175 rings_rpc::error::Error::InvalidMethod => Error::InvalidMethod,
176 rings_rpc::error::Error::RpcError(v) => Error::RemoteRpcError(v.to_string()),
177 rings_rpc::error::Error::InvalidSignature => Error::InvalidData,
178 rings_rpc::error::Error::InvalidHeaders => Error::InvalidHeaders,
179 _ => Error::UnknownRpcError,
180 }
181 }
182}
183
184#[cfg(feature = "browser")]
185impl From<Error> for wasm_bindgen::JsValue {
186 fn from(err: Error) -> Self {
187 wasm_bindgen::JsValue::from_str(&err.to_string())
188 }
189}
190
191#[cfg(test)]
192mod tests {
193 use super::*;
194 #[test]
195 fn test_error_code() {
196 let err = Error::RemoteRpcError("Test".to_string());
197 assert_eq!(err.code(), 100);
198 }
199}