aerospike/errors.rs
1// Copyright 2015-2020 Aerospike, Inc.
2//
3// Portions may be licensed to Aerospike, Inc. under one or more contributor
4// license agreements.
5//
6// Licensed under the Apache License, Version 2.0 (the "License"); you may not
7// use this file except in compliance with the License. You may obtain a copy of
8// the License at http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13// License for the specific language governing permissions and limitations under
14// the License.
15
16//! Error and Result types for the Aerospike client.
17//!
18//! # Examples
19//!
20//! Handling an error returned by the client.
21//!
22//! ```rust
23//! use aerospike::*;
24//!
25//! let hosts = std::env::var("AEROSPIKE_HOSTS").unwrap_or("localhost".into());
26//! let policy = ClientPolicy::default();
27//! let client = Client::new(&policy, &hosts).expect("Failed to connect to cluster");
28//! let key = as_key!("test", "test", "someKey");
29//! match client.get(&ReadPolicy::default(), &key, Bins::None) {
30//! Ok(record) => {
31//! match record.time_to_live() {
32//! None => println!("record never expires"),
33//! Some(duration) => println!("ttl: {} secs", duration.as_secs()),
34//! }
35//! },
36//! Err(Error(ErrorKind::ServerError(ResultCode::KeyNotFoundError), _)) => {
37//! println!("No such record: {}", key);
38//! },
39//! Err(err) => {
40//! println!("Error fetching record: {}", err);
41//! for err in err.iter().skip(1) {
42//! println!("Caused by: {}", err);
43//! }
44//! // The backtrace is not always generated. Try to run this example
45//! // with `RUST_BACKTRACE=1`.
46//! if let Some(backtrace) = err.backtrace() {
47//! println!("Backtrace: {:?}", backtrace);
48//! }
49//! }
50//! }
51//! ```
52
53#![allow(missing_docs)]
54
55use crate::ResultCode;
56
57error_chain! {
58
59// Automatic conversions between this error chain and other error types not defined by the
60// `error_chain!`.
61 foreign_links {
62 Base64(::base64::DecodeError)
63 #[doc = "Error decoding Base64 encoded value"];
64 InvalidUtf8(::std::str::Utf8Error)
65 #[doc = "Error interpreting a sequence of u8 as a UTF-8 encoded string."];
66 Io(::std::io::Error)
67 #[doc = "Error during an I/O operation"];
68 MpscRecv(::std::sync::mpsc::RecvError)
69 #[doc = "Error returned from the `recv` function on an MPSC `Receiver`"];
70 ParseAddr(::std::net::AddrParseError)
71 #[doc = "Error parsing an IP or socket address"];
72 ParseInt(::std::num::ParseIntError)
73 #[doc = "Error parsing an integer"];
74 PwHash(::pwhash::error::Error)
75 #[doc = "Error returned while hashing a password for user authentication"];
76 }
77
78// Additional `ErrorKind` variants.
79 errors {
80
81/// The client received a server response that it was not able to process.
82 BadResponse(details: String) {
83 description("Bad Server Response")
84 display("Bad Server Response: {}", details)
85 }
86
87/// The client was not able to communicate with the cluster due to some issue with the
88/// network connection.
89 Connection(details: String) {
90 description("Network Connection Issue")
91 display("Unable to communicate with server cluster: {}", details)
92 }
93
94/// One or more of the arguments passed to the client are invalid.
95 InvalidArgument(details: String) {
96 description("Invalid Argument")
97 display("Invalid argument: {}", details)
98 }
99
100/// Cluster node is invalid.
101 InvalidNode(details: String) {
102 description("Invalid cluster node")
103 display("Invalid cluster node: {}", details)
104 }
105
106/// Exceeded max. number of connections per node.
107 NoMoreConnections {
108 description("Too many connections")
109 display("Too many connections")
110 }
111
112/// Server responded with a response code indicating an error condition.
113 ServerError(rc: ResultCode) {
114 description("Server Error")
115 display("Server error: {}", rc.into_string())
116 }
117
118/// Error returned when executing a User-Defined Function (UDF) resulted in an error.
119 UdfBadResponse(details: String) {
120 description("UDF Bad Response")
121 display("UDF Bad Response: {}", details)
122 }
123
124/// Error returned when a tasked timeed out before it could be completed.
125 Timeout(details: String) {
126 description("Timeout")
127 display("Timeout: {}", details)
128 }
129 }
130}
131
132macro_rules! log_error_chain {
133 ($err:expr, $($arg:tt)*) => {
134 error!($($arg)*);
135 error!("Error: {}", $err);
136 for e in $err.iter().skip(1) {
137 error!("caused by: {}", e);
138 }
139 if let Some(backtrace) = $err.backtrace() {
140 error!("backtrace: {:?}", backtrace);
141 }
142 };
143}