ovs_unixctl/
error.rs

1use std::io;
2
3use serde_json;
4use thiserror;
5
6/// A unified error type for anything returned by a method in this crate.
7#[derive(thiserror::Error, Debug)]
8#[non_exhaustive]
9pub enum Error {
10    /// Something in the jsonrpc protocol failed
11    #[error("jsonrpc protocol error: {0}")]
12    Protocol(String),
13    /// Serialization or deserialization of data failed
14    #[error("(de/)serialization error: {0}")]
15    Serialize(serde_json::Error),
16    /// An error occurred in the socket I/O handling
17    #[error("input/output socket error: {0}")]
18    Socket(#[from] io::Error),
19    /// The connection timed-out waiting for a response
20    #[error("connection timeout")]
21    Timeout,
22    /// The remote peer returned an error
23    #[error("command {cmd}({params}) returns error: {error}")]
24    Command {
25        cmd: String,
26        params: String,
27        error: String,
28    },
29    /// An error occurred when trying to find the right unix socket
30    #[error("socket not found: {0}")]
31    SocketNotFound(String),
32    /// OpenvSwitch is not running
33    #[error("OpenvSwitch is not running")]
34    OvsNotRunning,
35    /// A builtin OpenvSwitch command returned invalid data
36    #[error("{cmd} returned invalid data ({response}): {error}")]
37    OvsInvalidResponse {
38        cmd: String,
39        response: String,
40        error: String,
41    },
42}
43
44impl From<serde_json::Error> for Error {
45    fn from(error: serde_json::Error) -> Error {
46        // serde_json errors can encapsulate IO errors.
47        use serde_json::error::Category::*;
48        match error.classify() {
49            Io => Error::Socket(error.into()),
50            _ => Error::Serialize(error),
51        }
52    }
53}