neon_serde3/
errors.rs

1//! Defines error handling types used by the create
2//! uses the `error-chain` create for generation
3
4use neon;
5use serde::{de, ser};
6use std::{convert::From, error, fmt, fmt::Display, result};
7
8pub type Result<T> = result::Result<T, Error>;
9
10#[derive(Debug)]
11pub enum Error {
12    /// nodejs has a hard coded limit on string length
13    /// trying to serialize a string that is too long will result in an error
14    StringTooLong { len: usize },
15
16    /// when deserializing to a boolean `false` `undefined` `null` `number`
17    /// are valid inputs
18    /// any other types will result in error
19    UnableToCoerce { to_type: &'static str },
20
21    /// occurs when deserializing a char from an empty string
22    EmptyString,
23
24    /// occurs when deserializing a char from a sting with
25    /// more than one character
26    StringTooLongForChar { len: usize },
27
28    /// occurs when a deserializer expects a `null` or `undefined`
29    /// property and found another type
30    ExpectingNull,
31
32    /// occurs when deserializing to an enum and the source object has
33    /// a none-1 number of properties
34    InvalidKeyType { key: String },
35
36    /// an internal deserialization error from an invalid array
37    ArrayIndexOutOfBounds { index: u32, length: u32 },
38
39    #[doc(hidden)]
40    /// This type of object is not supported
41    NotImplemented { name: &'static str },
42
43    /// A JS exception was thrown
44    Js { throw: neon::result::Throw },
45
46    /// failed to convert something to f64
47    CastError,
48
49    /// Generic serialize error
50    Serialize { msg: String },
51
52    /// Generic deserialize error
53    Deserialize { msg: String },
54}
55
56impl error::Error for Error {}
57
58impl Display for Error {
59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60        match self {
61            Error::StringTooLong { len } => {
62                "String too long for nodejs len: ".fmt(f)?;
63                len.fmt(f)
64            }
65            Error::UnableToCoerce { to_type } => {
66                "Unable to coerce value to type: ".fmt(f)?;
67                to_type.fmt(f)
68            }
69            Error::EmptyString => "EmptyString".fmt(f),
70            Error::StringTooLongForChar { len } => {
71                "String too long to be a char expected len: 1 got len: ".fmt(f)?;
72                len.fmt(f)
73            }
74            Error::ExpectingNull => "ExpectingNull".fmt(f),
75            Error::InvalidKeyType { key } => {
76                "Invalid type of key: '".fmt(f)?;
77                key.fmt(f)?;
78                '\''.fmt(f)
79            }
80            Error::ArrayIndexOutOfBounds { index, length } => {
81                "Array index out of bounds (".fmt(f)?;
82                index.fmt(f)?;
83                " of ".fmt(f)?;
84                length.fmt(f)?;
85                ")".fmt(f)
86            }
87            Error::NotImplemented { name } => {
88                "Not implemented: '".fmt(f)?;
89                name.fmt(f)?;
90                '\''.fmt(f)
91            }
92            Error::Js { throw: _ } => "JS exception".fmt(f),
93            Error::CastError => "Casting error".fmt(f),
94            Error::Serialize { msg } => {
95                "Serialize error: ".fmt(f)?;
96                msg.fmt(f)
97            }
98            Error::Deserialize { msg } => {
99                "Deserialize error: ".fmt(f)?;
100                msg.fmt(f)
101            }
102        }
103    }
104}
105
106impl ser::Error for Error {
107    fn custom<T: Display>(msg: T) -> Self {
108        Self::Serialize {
109            msg: msg.to_string(),
110        }
111    }
112}
113
114impl de::Error for Error {
115    fn custom<T: Display>(msg: T) -> Self {
116        Self::Deserialize {
117            msg: msg.to_string(),
118        }
119    }
120}
121
122impl From<neon::result::Throw> for Error {
123    fn from(throw: neon::result::Throw) -> Self {
124        Error::Js { throw }
125    }
126}