azure_speech/
error.rs

1use serde::Deserialize;
2use std::fmt::{Debug, Display};
3use std::result;
4use std::sync::PoisonError;
5
6/// Result type for the library.
7pub type Result<T> = result::Result<T, Error>;
8
9#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
10/// Error enum, used to represent errors in the library.
11pub enum Error {
12    IOError(String),
13    InvalidResponse(String),
14    ParseError(String),
15    InternalError(String),
16    RuntimeError(String),
17    ServerDisconnect(String),
18    ConnectionError(String),
19    Forbidden,
20    TooManyRequests,
21    BadRequest,
22}
23
24impl From<tokio_websockets::Error> for Error {
25    fn from(err: tokio_websockets::Error) -> Error {
26        Error::ConnectionError(err.to_string())
27    }
28}
29
30impl From<url::ParseError> for Error {
31    fn from(e: url::ParseError) -> Self {
32        Error::ParseError(e.to_string())
33    }
34}
35
36impl<T: Debug> From<PoisonError<T>> for Error {
37    fn from(e: PoisonError<T>) -> Self {
38        Error::InternalError(e.to_string())
39    }
40}
41
42impl From<serde_json::Error> for Error {
43    fn from(e: serde_json::Error) -> Self {
44        Error::ParseError(e.to_string())
45    }
46}
47
48impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
49    fn from(e: tokio::sync::mpsc::error::SendError<T>) -> Self {
50        Error::InternalError(e.to_string())
51    }
52}
53
54impl From<&str> for Error {
55    fn from(s: &str) -> Self {
56        Error::InternalError(s.to_string())
57    }
58}
59
60impl From<String> for Error {
61    fn from(s: String) -> Self {
62        Error::InternalError(s)
63    }
64}
65
66impl From<std::io::Error> for Error {
67    fn from(e: std::io::Error) -> Self {
68        Error::IOError(e.to_string())
69    }
70}
71
72impl Display for Error {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        match self {
75            Self::IOError(s) => write!(f, "IO error: {s}"),
76            Self::InvalidResponse(s) => write!(f, "Invalid response from server: {s}"),
77            Self::ParseError(s) => write!(f, "Failed to parse response from server: {s}"),
78            Self::InternalError(s) => write!(f, "Internal error: {s}"),
79            Self::RuntimeError(s) => write!(f, "Runtime error: {s}"),
80            Self::ServerDisconnect(s) => write!(f, "Disconnected from server: {s}"),
81            Self::ConnectionError(s) => write!(f, "Server connection closed due to error: {s}"),
82            Self::Forbidden => f.write_str("Invalid credentials"),
83            Self::TooManyRequests => f.write_str("Rate limited"),
84            Self::BadRequest => f.write_str("Malformed request"),
85        }
86    }
87}
88
89impl std::error::Error for Error {}