use std::io;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("unauthorized: {0}")]
Unauthorized(String),
#[error("bad request: {0}")]
BadRequest(String),
#[error("not found: {0}")]
NotFound(String),
#[error("conflict: {0}")]
Conflict(String),
#[error("payload too large: {0}")]
PayloadTooLarge(String),
#[error("http {status}: {message}")]
Http {
status: u16,
message: String,
},
#[error("network: {0}")]
Network(String),
#[error("timed out")]
TimedOut,
#[error("invalid server response: {0}")]
InvalidResponse(String),
#[error("id missing: {0}")]
IdMissing(&'static str),
#[error("invalid bitrate: {0}")]
InvalidBitrate(String),
#[error("device auth: {0}")]
DeviceAuth(String),
#[error("ynison: {message}")]
Ynison {
message: String,
retry_after: Option<std::time::Duration>,
},
#[error("io: {0}")]
Io(#[from] io::Error),
}
impl Error {
pub(crate) fn from_status(status: u16, message: String) -> Self {
match status {
401 | 403 => Self::Unauthorized(message),
400 => Self::BadRequest(message),
404 => Self::NotFound(message),
409 => Self::Conflict(message),
413 => Self::PayloadTooLarge(message),
502 => Self::Network("bad gateway".into()),
_ => Self::Http { status, message },
}
}
#[cfg(feature = "realtime")]
pub(crate) fn ynison(message: impl Into<String>) -> Self {
Self::Ynison {
message: message.into(),
retry_after: None,
}
}
#[must_use]
pub fn is_retryable(&self) -> bool {
match self {
Self::TimedOut | Self::Network(_) => true,
Self::Http { status, .. } => *status >= 500,
_ => false,
}
}
#[must_use]
pub fn retry_after(&self) -> Option<std::time::Duration> {
match self {
Self::Ynison { retry_after, .. } => *retry_after,
_ => None,
}
}
}
impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
if err.is_timeout() {
return Self::TimedOut;
}
if err.is_decode() {
return Self::InvalidResponse(err.to_string());
}
Self::Network(err.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Self::InvalidResponse(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn status_maps_to_the_matching_variant() {
assert!(matches!(
Error::from_status(401, String::new()),
Error::Unauthorized(_)
));
assert!(matches!(
Error::from_status(403, String::new()),
Error::Unauthorized(_)
));
assert!(matches!(
Error::from_status(400, String::new()),
Error::BadRequest(_)
));
assert!(matches!(
Error::from_status(404, String::new()),
Error::NotFound(_)
));
assert!(matches!(
Error::from_status(409, String::new()),
Error::Conflict(_)
));
assert!(matches!(
Error::from_status(413, String::new()),
Error::PayloadTooLarge(_)
));
assert!(matches!(
Error::from_status(502, String::new()),
Error::Network(_)
));
assert!(matches!(
Error::from_status(418, String::new()),
Error::Http { status: 418, .. }
));
}
#[test]
fn only_transport_failures_are_retryable() {
assert!(Error::TimedOut.is_retryable());
assert!(Error::Network("reset".into()).is_retryable());
assert!(
Error::Http {
status: 503,
message: String::new()
}
.is_retryable()
);
assert!(!Error::BadRequest(String::new()).is_retryable());
assert!(!Error::NotFound(String::new()).is_retryable());
assert!(!Error::Conflict(String::new()).is_retryable());
assert!(!Error::PayloadTooLarge(String::new()).is_retryable());
assert!(!Error::Unauthorized(String::new()).is_retryable());
assert!(
!Error::Http {
status: 418,
message: String::new()
}
.is_retryable()
);
}
#[test]
fn retry_after_is_carried_only_by_ynison_go_away() {
let go_away = Error::Ynison {
message: "temporary unavailable".into(),
retry_after: Some(std::time::Duration::from_secs(30)),
};
assert_eq!(
go_away.retry_after(),
Some(std::time::Duration::from_secs(30))
);
let plain = Error::Ynison {
message: "boom".into(),
retry_after: None,
};
assert_eq!(plain.retry_after(), None);
assert_eq!(Error::TimedOut.retry_after(), None);
}
}