1use 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 StringTooLong { len: usize },
15
16 UnableToCoerce { to_type: &'static str },
20
21 EmptyString,
23
24 StringTooLongForChar { len: usize },
27
28 ExpectingNull,
31
32 InvalidKeyType { key: String },
35
36 ArrayIndexOutOfBounds { index: u32, length: u32 },
38
39 #[doc(hidden)]
40 NotImplemented { name: &'static str },
42
43 Js { throw: neon::result::Throw },
45
46 CastError,
48
49 Serialize { msg: String },
51
52 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}