nsq_client/
error.rs

1// MIT License
2// 
3// Copyright (c) 2019-2021 Alessandro Cresto Miseroglio <alex179ohm@gmail.com>
4// Copyright (c) 2019-2021 Tangram Technologies S.R.L. <https://tngrm.io>
5// 
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12// 
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15// 
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23
24use std::str;
25use std::{error, fmt, io};
26use std::time::Duration;
27
28use futures::sync::{mpsc, oneshot};
29
30use codec;
31
32#[derive(Debug)]
33pub enum Error {
34    /// A Non-Specific internal error than prevented and operation from completing
35    Internal(String),
36
37    /// An IO error
38    IO(io::Error),
39
40    /// An parsing/serialising error occurred
41    Value(String, Option<codec::Cmd>),
42
43    /// An critical Unexpected Error
44    Unexpected(String),
45
46    /// End of Stream connection is broken
47    EndOfStream,
48
49    /// receive error during reconnecting
50    NotConnected,
51
52    /// Cancel all writers after connection get dropped
53    Disconnected,
54
55    /// Remote error
56    Remote(String),
57
58    /// Processing error on Handler
59    Processing(Duration, String),
60}
61
62pub fn internal<T: Into<String>>(msg: T) -> Error {
63    Error::Internal(msg.into())
64}
65
66pub fn value<T: Into<String>>(msg: T, val: codec::Cmd) -> Error {
67    Error::Value(msg.into(), Some(val))
68}
69
70impl From<io::Error> for Error {
71    fn from(err: io::Error) -> Error {
72        Error::IO(err)
73    }
74}
75
76impl From<oneshot::Canceled> for Error {
77    fn from(err: oneshot::Canceled) -> Error {
78        Error::Unexpected(format!("Oneshot was cancelled before use: {}", err))
79    }
80}
81
82impl<T: 'static + Send> From<mpsc::SendError<T>> for Error {
83    fn from(err: mpsc::SendError<T>) -> Error {
84        Error::Unexpected(format!("Cannot write to channel: {}", err))
85    }
86}
87
88impl error::Error for Error {
89    fn description(&self) -> &str {
90        match *self {
91            Error::IO(ref err) => err.description(),
92            Error::Value(ref s, _) => s,
93            Error::Unexpected(ref s) => s,
94            Error::Internal(ref s) => s,
95            Error::EndOfStream => "End of Stream",
96            Error::Remote(ref s) => s,
97            Error::NotConnected => "Not Connected",
98            Error::Disconnected => "Disconnected",
99            Error::Processing(_, ref s) => s,
100        }
101    }
102
103    fn cause(&self) -> Option<&error::Error> {
104        match *self {
105            Error::IO(ref err) => Some(err),
106            Error::Value(_, _) => None,
107            Error::Internal(_) => None,
108            Error::Unexpected(_) => None,
109            Error::EndOfStream => None,
110            Error::Remote(_) => None,
111            Error::NotConnected => None,
112            Error::Disconnected => None,
113            Error::Processing(_, _) => None,
114        }
115    }
116}
117
118impl fmt::Display for Error {
119    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120        use std::error::Error;
121        fmt::Display::fmt(self.description(), f)
122    }
123}