azure_speech/
error.rs

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
use serde::Deserialize;
use std::fmt::{Debug, Display};
use std::result;
use std::sync::PoisonError;

/// Result type for the library.
pub type Result<T> = result::Result<T, Error>;

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
/// Error enum, used to represent errors in the library.
pub enum Error {
    IOError(String),
    InvalidResponse(String),
    ParseError(String),
    InternalError(String),
    RuntimeError(String),
    ServerDisconnect(String),
    ConnectionError(String),
    Forbidden,
    TooManyRequests,
    BadRequest,
}

impl From<tokio_websockets::Error> for Error {
    fn from(err: tokio_websockets::Error) -> Error {
        Error::ConnectionError(err.to_string())
    }
}

impl From<url::ParseError> for Error {
    fn from(e: url::ParseError) -> Self {
        Error::ParseError(e.to_string())
    }
}

impl<T: Debug> From<PoisonError<T>> for Error {
    fn from(e: PoisonError<T>) -> Self {
        Error::InternalError(e.to_string())
    }
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Self {
        Error::ParseError(e.to_string())
    }
}

impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
    fn from(e: tokio::sync::mpsc::error::SendError<T>) -> Self {
        Error::InternalError(e.to_string())
    }
}

impl From<&str> for Error {
    fn from(s: &str) -> Self {
        Error::InternalError(s.to_string())
    }
}

impl From<String> for Error {
    fn from(s: String) -> Self {
        Error::InternalError(s)
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::IOError(e.to_string())
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::IOError(s) => write!(f, "IO error: {s}"),
            Self::InvalidResponse(s) => write!(f, "Invalid response from server: {s}"),
            Self::ParseError(s) => write!(f, "Failed to parse response from server: {s}"),
            Self::InternalError(s) => write!(f, "Internal error: {s}"),
            Self::RuntimeError(s) => write!(f, "Runtime error: {s}"),
            Self::ServerDisconnect(s) => write!(f, "Disconnected from server: {s}"),
            Self::ConnectionError(s) => write!(f, "Server connection closed due to error: {s}"),
            Self::Forbidden => f.write_str("Invalid credentials"),
            Self::TooManyRequests => f.write_str("Rate limited"),
            Self::BadRequest => f.write_str("Malformed request"),
        }
    }
}

impl std::error::Error for Error {}