zksync_error/
serialized.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// AUTOGENERATED BASED ON A SET OF JSON FILES, DO NOT EDIT MANUALLY
//
use crate::{
    error::{IError, IUnifiedError},
    identifier::{Identifier, StructuredErrorCode},
    kind::Kind,
    packed::PackedError,
    untyped::UntypedErrorObject,
};
use std::error::Error;
use std::fmt::Debug;
pub type ErrorCode = u32;
#[derive(Clone, Debug, Eq, PartialEq, serde :: Serialize, serde :: Deserialize)]
pub struct SerializedError {
    pub code: ErrorCode,
    pub message: String,
    pub data: serde_json::Value,
}
impl SerializedError {
    pub fn new_custom(
        kind: Kind,
        code: ErrorCode,
        message: impl Into<String>,
        unified_error_json: serde_json::Value,
    ) -> Self {
        SerializedError {
            code: Identifier::new(kind, code).encode(),
            message: message.into(),
            data: unified_error_json,
        }
    }
}
impl std::fmt::Display for SerializedError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(
            "{{ code: {}; message: \"{}\"; data: \"{}\"}}",
            self.code, self.message, self.data
        ))
    }
}
pub fn serialize<U>(error: PackedError<U>) -> Result<SerializedError, serde_json::Error>
where
    U: serde::Serialize + Clone + Debug,
{
    Ok(SerializedError {
        code: error.identifier.encode(),
        message: error.message,
        data: serde_json::value::to_value(&error.data)?,
    })
}
pub fn serialize_ref<U>(error: &PackedError<U>) -> Result<SerializedError, serde_json::Error>
where
    U: serde::Serialize + Clone + Debug,
{
    Ok(SerializedError {
        code: error.identifier.encode(),
        message: error.message.clone(),
        data: serde_json::value::to_value(&error.data)?,
    })
}
pub fn unpack_untyped(se: &SerializedError) -> Result<UntypedErrorObject, serde_json::Error> {
    let identifier = Identifier::decode(StructuredErrorCode::decode(se.code)).unwrap();
    let skip_domain = se.data.as_object().unwrap().values().next().unwrap();
    let skip_subdomain = skip_domain.as_object().unwrap().values().next().unwrap();
    let (name, value) = skip_subdomain.as_object().unwrap().iter().next().unwrap();
    let fields: serde_json::Map<String, serde_json::Value> = value.as_object().unwrap().clone();
    Ok(UntypedErrorObject {
        identifier,
        name: name.clone(),
        fields,
        raw: se.data.clone(),
    })
}
pub fn unpack_typed<T>(se: &SerializedError) -> Result<T, serde_json::Error>
where
    T: IUnifiedError<T> + serde::Serialize + for<'de> serde::Deserialize<'de> + Clone,
{
    serde_json::value::from_value(se.data.clone())
}
impl Error for SerializedError {}
impl IError<UntypedErrorObject> for SerializedError {
    fn get_identifier(&self) -> Identifier {
        Identifier::decode(StructuredErrorCode::decode(self.code)).unwrap()
    }
    fn get_message(&self) -> String {
        self.message.clone()
    }
    fn get_data(&self) -> UntypedErrorObject {
        unpack_untyped(self).expect("Internal error")
    }
}