fog_pack/
error.rs

1//! Library error types.
2//!
3use fog_crypto::{hash::Hash, CryptoError};
4use std::fmt;
5
6use serde::{de, ser};
7
8/// A fog-pack Result, normally returning a fog-pack [`Error`].
9pub type Result<T, E = Error> = std::result::Result<T, E>;
10
11#[allow(unused)]
12#[derive(Clone, Debug)]
13enum ValidateError {
14    /// Validation failed inside an array
15    InArray {
16        index: usize,
17        err: Box<ValidateError>,
18    },
19    /// Validation failed inside a map
20    InMap {
21        key: String,
22        err: Box<ValidateError>,
23    },
24    /// Validation failed inside an enum
25    InEnum {
26        name: String,
27        err: Box<ValidateError>,
28    },
29    /// Validation of an array failed at a specific index
30    FailArray { index: usize, err: String },
31    /// Validation of a map failed at a specific key
32    FailMap { key: String, err: String },
33    /// All of the available "multi" validators failed
34    FailMulti { errs: Vec<ValidateError> },
35    /// The core validation error
36    FailValue {
37        failure: String,
38        value: crate::value::Value,
39    },
40    /// Some other fog-pack error occurred in here
41    SubError(Box<Error>),
42}
43
44/// A fog-pack error. Encompasses any issues that can happen during validation,
45/// encoding, or decoding.
46#[derive(Clone, PartialEq, Eq, Debug)]
47pub enum Error {
48    /// Occurs when a subtype is using a version format that is no longer accepted. This is mainly
49    /// for recognizing when the Cryptographic types and signatures use old, no longer accepted
50    /// algorithms.
51    OldVersion(String),
52    /// Occurs when a schema doesn't match the document's schema, a Schema was used when one isn't
53    /// specified by the document, or a NoSchema was used when a document specified a schema.
54    SchemaMismatch {
55        /// The actual schema of the document
56        actual: Option<Hash>,
57        /// The expected schema of the document
58        expected: Option<Hash>,
59    },
60    /// Occurs when serde serialization or deserialization fails
61    SerdeFail(String),
62    /// Occurs when the header (compression marker and optional schema) failed to parse correctly.
63    BadHeader(String),
64    /// Occurs when zstd compression fails, possibly due to a dictionary not being present in a
65    /// schema, a checksum failing, or any of the other zstd failure modes.
66    FailDecompress(String),
67    /// Document/Entry/Query was greater than maximum allowed size on decode
68    LengthTooLong {
69        /// The maximum allowed size
70        max: usize,
71        /// The object's actual size
72        actual: usize,
73    },
74    /// Document/Entry/Query ended too early.
75    LengthTooShort {
76        /// What step of the decoding we were on when it failed.
77        step: &'static str,
78        /// The actual length of the object
79        actual: usize,
80        /// The remaining length of the object that we were expecting
81        expected: usize,
82    },
83    /// Signature attached to end of Document/Entry/Query didn't validate
84    BadSignature,
85    /// Basic fog-pack encoding failure
86    BadEncode(String),
87    /// Schema validation failure.
88    FailValidate(String),
89    /// Failure within the cryptographic submodule.
90    CryptoError(CryptoError),
91    /// Schema or validation hit some parsing limit.
92    ParseLimit(String),
93}
94
95impl fmt::Display for Error {
96    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97        match *self {
98            Error::OldVersion(ref err) => write!(f, "Old version: {}", err),
99            Error::SchemaMismatch {
100                ref actual,
101                ref expected,
102            } => match (actual, expected) {
103                (Some(actual), Some(expected)) => write!(
104                    f,
105                    "Expected schema {}, but document used schema {}",
106                    expected, actual
107                ),
108                (Some(actual), None) => {
109                    write!(f, "Expected no schema, but document used schema {}", actual)
110                }
111                (None, Some(expected)) => write!(
112                    f,
113                    "Expected schema {}, but document used no schema",
114                    expected
115                ),
116                (None, None) => write!(
117                    f,
118                    "Expected and got no schema (this should not have been an error)"
119                ),
120            },
121            Error::SerdeFail(ref msg) => f.write_str(msg),
122            Error::BadHeader(ref err) => write!(f, "Data has bad header format: {}", err),
123            Error::FailDecompress(ref err) => write!(f, "Failed decompression step: {}", err),
124            Error::LengthTooLong { max, actual } => write!(
125                f,
126                "Data too long: was {} bytes, maximum allowed is {}",
127                actual, max
128            ),
129            Error::LengthTooShort {
130                step,
131                actual,
132                expected,
133            } => write!(
134                f,
135                "Expected data length {}, but got {} on step [{}]",
136                expected, actual, step
137            ),
138            Error::BadSignature => write!(f, "A signature failed to verify"),
139            Error::BadEncode(ref err) => write!(f, "Basic data encoding failure: {}", err),
140            Error::FailValidate(ref err) => write!(f, "Failed validation: {}", err),
141            Error::CryptoError(_) => write!(f, "Cryptographic Error"),
142            Error::ParseLimit(ref err) => write!(f, "Hit parsing limit: {}", err),
143        }
144    }
145}
146
147impl std::error::Error for Error {
148    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
149        match *self {
150            Error::CryptoError(ref err) => Some(err),
151            _ => None,
152        }
153    }
154}
155
156impl std::convert::From<CryptoError> for Error {
157    fn from(e: CryptoError) -> Self {
158        Self::CryptoError(e)
159    }
160}
161
162impl ser::Error for Error {
163    fn custom<T: fmt::Display>(msg: T) -> Self {
164        Error::SerdeFail(msg.to_string())
165    }
166}
167
168impl de::Error for Error {
169    fn custom<T: fmt::Display>(msg: T) -> Self {
170        Error::SerdeFail(msg.to_string())
171    }
172}