Skip to main content

cueball/
error.rs

1// Copyright 2019 Joyent, Inc.
2
3use std::error::Error as StdError;
4use std::fmt;
5
6const CLAIM_FAILURE_STR: &str = "Unable to retrieve a connection within the \
7                                 claim timeout";
8const BACKEND_NO_CONNECTION_STR: &str = "Found a backend key with no \
9                                         associated connection";
10const CONNECTION_RETRIEVAL_FAILURE_STR: &str = "Unable to retrieve a \
11                                                connection";
12const STOP_CALLED_BY_CLONE_STR: &str =
13    "ConnectionPool clones may not stop the \
14     connection pool.";
15const DUMMY_ERROR_STR: &str = "dummy error";
16
17#[derive(Debug)]
18/// The cueball `Error` type is an `enum` that represents the different errors
19/// that may be returned by the cueball API.
20pub enum Error {
21    /// The call to `claim` to failed to retrieve a connection within the specified timeout period.
22    ClaimFailure,
23    /// The `stop` function was called on a pool clone. Only the original
24    /// connction pool instance may stop a connection pool. Thread `JoinHandles`
25    /// may not be cloned and therefore invocation of this function by a clone
26    /// of the pool results in an error.
27    StopCalledByClone,
28    /// A backend key was found with no associated connection. This error should
29    /// never happen and is only represented for completeness. Please file a bug
30    /// if it is encountered.
31    BackendWithNoConnection,
32    /// A connection could not be retrieved from the connection pool even though
33    /// the connection pool accounting indicated one should be available. This
34    /// error should never happen and is only represented for
35    /// completeness. Please file a bug if it is encountered.
36    ConnectionRetrievalFailure,
37    // For internal pool use only
38    #[doc(hidden)]
39    DummyError,
40}
41
42impl fmt::Display for Error {
43    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
44        match self {
45            Error::ClaimFailure => CLAIM_FAILURE_STR.fmt(fmt),
46            Error::BackendWithNoConnection => {
47                BACKEND_NO_CONNECTION_STR.fmt(fmt)
48            }
49            Error::ConnectionRetrievalFailure => {
50                CONNECTION_RETRIEVAL_FAILURE_STR.fmt(fmt)
51            }
52            Error::StopCalledByClone => STOP_CALLED_BY_CLONE_STR.fmt(fmt),
53            Error::DummyError => DUMMY_ERROR_STR.fmt(fmt),
54        }
55    }
56}
57
58impl StdError for Error {
59    fn description(&self) -> &str {
60        match self {
61            Error::ClaimFailure => CLAIM_FAILURE_STR,
62            Error::BackendWithNoConnection => BACKEND_NO_CONNECTION_STR,
63            Error::ConnectionRetrievalFailure => {
64                CONNECTION_RETRIEVAL_FAILURE_STR
65            }
66            Error::StopCalledByClone => STOP_CALLED_BY_CLONE_STR,
67            Error::DummyError => DUMMY_ERROR_STR,
68        }
69    }
70
71    fn source(&self) -> Option<&(dyn StdError + 'static)> {
72        None
73    }
74}