1use std::ffi::OsString;
6use std::fmt::{self, Display};
7use std::io;
8use std::result;
9
10use rkv::StoreError;
11
12pub type Result<T, E = Error> = result::Result<T, E>;
20
21#[derive(Debug)]
28#[non_exhaustive]
29pub enum ErrorKind {
30 Lifetime(i32),
32
33 IoError(io::Error),
35
36 Rkv(StoreError),
38
39 Json(serde_json::error::Error),
41
42 TimeUnit(i32),
44
45 MemoryUnit(i32),
47
48 HistogramType(i32),
50
51 OsString(OsString),
53
54 Utf8Error,
56
57 InvalidConfig,
59
60 NotInitialized,
62
63 PingBodyOverflow(usize),
65}
66
67#[derive(Debug)]
71pub struct Error {
72 kind: ErrorKind,
73}
74
75impl Error {
76 pub fn utf8_error() -> Error {
80 Error {
81 kind: ErrorKind::Utf8Error,
82 }
83 }
84
85 pub fn not_initialized() -> Error {
87 Error {
88 kind: ErrorKind::NotInitialized,
89 }
90 }
91
92 pub fn kind(&self) -> &ErrorKind {
94 &self.kind
95 }
96}
97
98impl std::error::Error for Error {}
99
100impl Display for Error {
101 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102 use ErrorKind::*;
103 match self.kind() {
104 Lifetime(l) => write!(f, "Lifetime conversion from {} failed", l),
105 IoError(e) => write!(f, "An I/O error occurred: {}", e),
106 Rkv(e) => write!(f, "An Rkv error occurred: {}", e),
107 Json(e) => write!(f, "A JSON error occurred: {}", e),
108 TimeUnit(t) => write!(f, "TimeUnit conversion from {} failed", t),
109 MemoryUnit(m) => write!(f, "MemoryUnit conversion from {} failed", m),
110 HistogramType(h) => write!(f, "HistogramType conversion from {} failed", h),
111 OsString(s) => write!(f, "OsString conversion from {:?} failed", s),
112 Utf8Error => write!(f, "Invalid UTF-8 byte sequence in string"),
113 InvalidConfig => write!(f, "Invalid Glean configuration provided"),
114 NotInitialized => write!(f, "Global Glean object missing"),
115 PingBodyOverflow(s) => write!(
116 f,
117 "Ping request body size exceeded maximum size allowed: {}kB.",
118 s / 1024
119 ),
120 }
121 }
122}
123
124impl From<ErrorKind> for Error {
125 fn from(kind: ErrorKind) -> Error {
126 Error { kind }
127 }
128}
129
130impl From<io::Error> for Error {
131 fn from(error: io::Error) -> Error {
132 Error {
133 kind: ErrorKind::IoError(error),
134 }
135 }
136}
137
138impl From<StoreError> for Error {
139 fn from(error: StoreError) -> Error {
140 Error {
141 kind: ErrorKind::Rkv(error),
142 }
143 }
144}
145
146impl From<serde_json::error::Error> for Error {
147 fn from(error: serde_json::error::Error) -> Error {
148 Error {
149 kind: ErrorKind::Json(error),
150 }
151 }
152}
153
154impl From<OsString> for Error {
155 fn from(error: OsString) -> Error {
156 Error {
157 kind: ErrorKind::OsString(error),
158 }
159 }
160}
161
162impl From<std::convert::Infallible> for Error {
166 fn from(_: std::convert::Infallible) -> Error {
167 unreachable!()
168 }
169}