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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use bytes_utils::string::Utf8Error as BytesUtf8Error;
use futures::channel::oneshot::Canceled;
use redis_protocol::{error::RedisProtocolError, resp2::types::BytesFrame as Resp2Frame};
use semver::Error as SemverError;
use std::{
  borrow::{Borrow, Cow},
  convert::Infallible,
  error::Error,
  fmt,
  io::Error as IoError,
  num::{ParseFloatError, ParseIntError},
  str,
  str::Utf8Error,
  string::FromUtf8Error,
};
use url::ParseError;

/// An enum representing the type of error from Redis.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum RedisErrorKind {
  /// A fatal client configuration error. These errors will shutdown a client and break out of any reconnection
  /// attempts.
  Config,
  /// An authentication error.
  Auth,
  /// An IO error with the underlying connection.
  IO,
  /// An invalid command, such as trying to perform a `set` command on a client after calling `subscribe`.
  InvalidCommand,
  /// An invalid argument or set of arguments to a command.
  InvalidArgument,
  /// An invalid URL error.
  Url,
  /// A protocol error such as an invalid or unexpected frame from the server.
  Protocol,
  /// A TLS error.
  #[cfg(any(
    feature = "enable-native-tls",
    feature = "enable-rustls",
    feature = "enable-rustls-ring"
  ))]
  #[cfg_attr(
    docsrs,
    doc(cfg(any(
      feature = "enable-native-tls",
      feature = "enable-rustls",
      feature = "enable-rustls-ring"
    )))
  )]
  Tls,
  /// An error indicating the request was canceled.
  Canceled,
  /// An unknown error.
  Unknown,
  /// A timeout error.
  Timeout,
  /// An error used to indicate that the cluster's state has changed. These errors will show up on the `on_error`
  /// error stream even though the client will automatically attempt to recover.
  Cluster,
  /// A parser error.
  Parse,
  /// An error communicating with redis sentinel.
  Sentinel,
  /// An error indicating a value was not found, often used when trying to cast a `nil` response from the server to a
  /// non-nullable type.
  NotFound,
  /// An error indicating that the caller should apply backpressure and retry the command.
  Backpressure,
  /// An error associated with a replica node.
  #[cfg(feature = "replicas")]
  #[cfg_attr(docsrs, doc(cfg(feature = "replicas")))]
  Replica,
}

impl RedisErrorKind {
  pub fn to_str(&self) -> &'static str {
    match *self {
      RedisErrorKind::Auth => "Authentication Error",
      RedisErrorKind::IO => "IO Error",
      RedisErrorKind::InvalidArgument => "Invalid Argument",
      RedisErrorKind::InvalidCommand => "Invalid Command",
      RedisErrorKind::Url => "Url Error",
      RedisErrorKind::Protocol => "Protocol Error",
      RedisErrorKind::Unknown => "Unknown Error",
      RedisErrorKind::Canceled => "Canceled",
      RedisErrorKind::Cluster => "Cluster Error",
      RedisErrorKind::Timeout => "Timeout Error",
      #[cfg(any(
        feature = "enable-native-tls",
        feature = "enable-rustls",
        feature = "enable-rustls-ring"
      ))]
      RedisErrorKind::Tls => "TLS Error",
      RedisErrorKind::Config => "Config Error",
      RedisErrorKind::Parse => "Parse Error",
      RedisErrorKind::Sentinel => "Sentinel Error",
      RedisErrorKind::NotFound => "Not Found",
      RedisErrorKind::Backpressure => "Backpressure",
      #[cfg(feature = "replicas")]
      RedisErrorKind::Replica => "Replica",
    }
  }
}

/// An error from Redis.
pub struct RedisError {
  /// Details about the specific error condition.
  details: Cow<'static, str>,
  /// The kind of error.
  kind:    RedisErrorKind,
}

impl Clone for RedisError {
  fn clone(&self) -> Self {
    RedisError::new(self.kind.clone(), self.details.clone())
  }
}

impl PartialEq for RedisError {
  fn eq(&self, other: &Self) -> bool {
    self.kind == other.kind && self.details == other.details
  }
}

impl Eq for RedisError {}

impl fmt::Debug for RedisError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "Redis Error - kind: {:?}, details: {}", self.kind, self.details)
  }
}

impl fmt::Display for RedisError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}: {}", self.kind.to_str(), self.details)
  }
}

#[doc(hidden)]
impl From<RedisProtocolError> for RedisError {
  fn from(e: RedisProtocolError) -> Self {
    RedisError::new(RedisErrorKind::Protocol, format!("{}", e))
  }
}

#[doc(hidden)]
impl From<()> for RedisError {
  fn from(_: ()) -> Self {
    RedisError::new(RedisErrorKind::Canceled, "Empty error.")
  }
}

#[doc(hidden)]
impl From<futures::channel::mpsc::SendError> for RedisError {
  fn from(e: futures::channel::mpsc::SendError) -> Self {
    RedisError::new(RedisErrorKind::Unknown, format!("{}", e))
  }
}

#[doc(hidden)]
impl From<tokio::sync::oneshot::error::RecvError> for RedisError {
  fn from(e: tokio::sync::oneshot::error::RecvError) -> Self {
    RedisError::new(RedisErrorKind::Unknown, format!("{}", e))
  }
}

#[doc(hidden)]
impl From<tokio::sync::broadcast::error::RecvError> for RedisError {
  fn from(e: tokio::sync::broadcast::error::RecvError) -> Self {
    RedisError::new(RedisErrorKind::Unknown, format!("{}", e))
  }
}

#[doc(hidden)]
impl<T: fmt::Display> From<tokio::sync::broadcast::error::SendError<T>> for RedisError {
  fn from(e: tokio::sync::broadcast::error::SendError<T>) -> Self {
    RedisError::new(RedisErrorKind::Unknown, format!("{}", e))
  }
}

#[doc(hidden)]
impl From<IoError> for RedisError {
  fn from(e: IoError) -> Self {
    RedisError::new(RedisErrorKind::IO, format!("{:?}", e))
  }
}

#[doc(hidden)]
impl From<ParseError> for RedisError {
  fn from(e: ParseError) -> Self {
    RedisError::new(RedisErrorKind::Url, format!("{:?}", e))
  }
}

#[doc(hidden)]
impl From<ParseFloatError> for RedisError {
  fn from(_: ParseFloatError) -> Self {
    RedisError::new(RedisErrorKind::Parse, "Invalid floating point number.")
  }
}

#[doc(hidden)]
impl From<ParseIntError> for RedisError {
  fn from(_: ParseIntError) -> Self {
    RedisError::new(RedisErrorKind::Parse, "Invalid integer string.")
  }
}

#[doc(hidden)]
impl From<FromUtf8Error> for RedisError {
  fn from(_: FromUtf8Error) -> Self {
    RedisError::new(RedisErrorKind::Parse, "Invalid UTF-8 string.")
  }
}

#[doc(hidden)]
impl From<Utf8Error> for RedisError {
  fn from(_: Utf8Error) -> Self {
    RedisError::new(RedisErrorKind::Parse, "Invalid UTF-8 string.")
  }
}

#[doc(hidden)]
impl<S> From<BytesUtf8Error<S>> for RedisError {
  fn from(e: BytesUtf8Error<S>) -> Self {
    e.utf8_error().into()
  }
}

#[doc(hidden)]
impl From<fmt::Error> for RedisError {
  fn from(e: fmt::Error) -> Self {
    RedisError::new(RedisErrorKind::Unknown, format!("{:?}", e))
  }
}

#[doc(hidden)]
impl From<Canceled> for RedisError {
  fn from(e: Canceled) -> Self {
    RedisError::new(RedisErrorKind::Canceled, format!("{}", e))
  }
}

#[doc(hidden)]
#[cfg(not(feature = "glommio"))]
impl From<tokio::task::JoinError> for RedisError {
  fn from(e: tokio::task::JoinError) -> Self {
    RedisError::new(RedisErrorKind::Unknown, format!("Spawn Error: {:?}", e))
  }
}

#[doc(hidden)]
#[cfg(feature = "glommio")]
impl<T: fmt::Debug> From<glommio::GlommioError<T>> for RedisError {
  fn from(e: glommio::GlommioError<T>) -> Self {
    RedisError::new(RedisErrorKind::Unknown, format!("{:?}", e))
  }
}

#[doc(hidden)]
#[cfg(feature = "glommio")]
impl From<oneshot::RecvError> for RedisError {
  fn from(_: oneshot::RecvError) -> Self {
    RedisError::new_canceled()
  }
}

#[doc(hidden)]
impl From<SemverError> for RedisError {
  fn from(e: SemverError) -> Self {
    RedisError::new(RedisErrorKind::Protocol, format!("Invalid Redis version: {:?}", e))
  }
}

#[doc(hidden)]
impl From<Infallible> for RedisError {
  fn from(e: Infallible) -> Self {
    warn!("Infallible error: {:?}", e);
    RedisError::new(RedisErrorKind::Unknown, "Unknown error.")
  }
}

#[doc(hidden)]
impl From<Resp2Frame> for RedisError {
  fn from(e: Resp2Frame) -> Self {
    match e {
      Resp2Frame::SimpleString(s) => match str::from_utf8(&s).ok() {
        Some("Canceled") => RedisError::new_canceled(),
        _ => RedisError::new(RedisErrorKind::Unknown, "Unknown frame error."),
      },
      _ => RedisError::new(RedisErrorKind::Unknown, "Unknown frame error."),
    }
  }
}

#[doc(hidden)]
#[cfg(feature = "enable-native-tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "enable-native-tls")))]
impl From<native_tls::Error> for RedisError {
  fn from(e: native_tls::Error) -> Self {
    RedisError::new(RedisErrorKind::Tls, format!("{:?}", e))
  }
}

#[doc(hidden)]
#[cfg(any(feature = "enable-rustls", feature = "enable-rustls-ring"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "enable-rustls", feature = "enable-rustls-ring"))))]
impl From<rustls::pki_types::InvalidDnsNameError> for RedisError {
  fn from(e: rustls::pki_types::InvalidDnsNameError) -> Self {
    RedisError::new(RedisErrorKind::Tls, format!("{:?}", e))
  }
}

#[doc(hidden)]
#[cfg(any(feature = "enable-rustls", feature = "enable-rustls-ring"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "enable-rustls", feature = "enable-rustls-ring"))))]
impl From<rustls::Error> for RedisError {
  fn from(e: rustls::Error) -> Self {
    RedisError::new(RedisErrorKind::Tls, format!("{:?}", e))
  }
}

#[doc(hidden)]
#[cfg(feature = "trust-dns-resolver")]
#[cfg_attr(docsrs, doc(cfg(feature = "trust-dns-resolver")))]
impl From<trust_dns_resolver::error::ResolveError> for RedisError {
  fn from(e: trust_dns_resolver::error::ResolveError) -> Self {
    RedisError::new(RedisErrorKind::IO, format!("{:?}", e))
  }
}

#[doc(hidden)]
#[cfg(feature = "dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "dns")))]
impl From<hickory_resolver::error::ResolveError> for RedisError {
  fn from(e: hickory_resolver::error::ResolveError) -> Self {
    RedisError::new(RedisErrorKind::IO, format!("{:?}", e))
  }
}

#[cfg(feature = "serde-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde-json")))]
impl From<serde_json::Error> for RedisError {
  fn from(e: serde_json::Error) -> Self {
    RedisError::new(RedisErrorKind::Parse, format!("{:?}", e))
  }
}

impl RedisError {
  /// Create a new Redis error with the provided details.
  pub fn new<T>(kind: RedisErrorKind, details: T) -> RedisError
  where
    T: Into<Cow<'static, str>>,
  {
    RedisError {
      kind,
      details: details.into(),
    }
  }

  /// Read the type of error without any associated data.
  pub fn kind(&self) -> &RedisErrorKind {
    &self.kind
  }

  /// Change the kind of the error.
  pub fn change_kind(&mut self, kind: RedisErrorKind) {
    self.kind = kind;
  }

  /// Read details about the error.
  pub fn details(&self) -> &str {
    self.details.borrow()
  }

  /// Create a new empty Canceled error.
  pub fn new_canceled() -> Self {
    RedisError::new(RedisErrorKind::Canceled, "Canceled.")
  }

  /// Create a new parse error with the provided details.
  pub(crate) fn new_parse<T>(details: T) -> Self
  where
    T: Into<Cow<'static, str>>,
  {
    RedisError::new(RedisErrorKind::Parse, details)
  }

  /// Create a new default backpressure error.
  pub(crate) fn new_backpressure() -> Self {
    RedisError::new(RedisErrorKind::Backpressure, "Max in-flight commands reached.")
  }

  /// Whether reconnection logic should be skipped in all cases.
  pub(crate) fn should_not_reconnect(&self) -> bool {
    matches!(self.kind, RedisErrorKind::Config | RedisErrorKind::Url)
  }

  /// Whether the error is a `Cluster` error.
  pub fn is_cluster(&self) -> bool {
    matches!(self.kind, RedisErrorKind::Cluster)
  }

  /// Whether the error is a `Canceled` error.
  pub fn is_canceled(&self) -> bool {
    matches!(self.kind, RedisErrorKind::Canceled)
  }

  /// Whether the error is a `Replica` error.
  #[cfg(feature = "replicas")]
  #[cfg_attr(docsrs, doc(cfg(feature = "replicas")))]
  pub fn is_replica(&self) -> bool {
    matches!(self.kind, RedisErrorKind::Replica)
  }

  /// Whether the error is a `NotFound` error.
  pub fn is_not_found(&self) -> bool {
    matches!(self.kind, RedisErrorKind::NotFound)
  }
}

impl Error for RedisError {
  fn source(&self) -> Option<&(dyn Error + 'static)> {
    None
  }
}