use crate::models;
use serde::{Deserialize, Serialize};
use std::error;
use std::fmt::Display;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("HTTP request failed: {0}")]
Http(#[from] reqwest::Error),
#[error("JSON serialization/deserialization failed: {0}")]
SerdeJson(#[from] serde_json::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Tidal Error: {0}")]
TidalError(TidalError),
#[error("No authz token available to refresh client authorization")]
NoAuthzToken,
#[error("No primary streaming URL available")]
NoPrimaryUrl,
#[error("Stream initialization error: {0}")]
StreamInitializationError(String),
#[error("No access token available - have you authorized the client?")]
NoAccessTokenAvailable,
#[error("User authentication required - please login first")]
UserAuthenticationRequired,
#[error("Hit rate limit backoff ceiling of {0}ms without recovery")]
RateLimitBackoffExceeded(u64),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TidalError {
TidalV2Error(TidalV2Error),
TidalV1Error(TidalV1Error),
UnknownError(TidalUnknownError),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TidalUnknownError {
pub status: u16,
pub message: String,
}
impl Display for TidalUnknownError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Status: {}, Message: {}", self.status, self.message)
}
}
impl Display for TidalError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TidalError::TidalV1Error(err) => write!(f, "{}", err),
TidalError::TidalV2Error(err) => write!(f, "{}", err),
TidalError::UnknownError(err) => write!(f, "{}", err),
}
}
}
#[derive(Debug, Serialize, Clone)]
pub struct TidalV1Error {
pub status: u16,
pub sub_status: u64,
pub user_message: String,
}
impl<'de> Deserialize<'de> for TidalV1Error {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value: serde_json::Value = serde_json::Value::deserialize(deserializer)?;
let status = value
.get("status")
.and_then(|v| v.as_u64())
.ok_or_else(|| serde::de::Error::custom("Missing or invalid 'status' field"))?
as u16;
let sub_status = value
.get("sub_status")
.or_else(|| value.get("subStatus"))
.and_then(|v| v.as_u64())
.ok_or_else(|| {
serde::de::Error::custom("Missing or invalid 'sub_status'/'subStatus' field")
})?;
let user_message = value
.get("user_message")
.or_else(|| value.get("userMessage"))
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
Ok(TidalV1Error {
status,
sub_status,
user_message,
})
}
}
impl Display for TidalV1Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Tidal API error: {} {} {}",
self.status, self.sub_status, self.user_message
)
}
}
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct TidalV2Error {
#[serde(rename = "errors", skip_serializing_if = "Option::is_none")]
pub errors: Option<Vec<ErrorObject>>,
#[serde(rename = "links", skip_serializing_if = "Option::is_none")]
pub links: Option<models::Links>,
}
impl TidalV2Error {
pub fn new() -> TidalV2Error {
TidalV2Error {
errors: None,
links: None,
}
}
}
impl Display for TidalV2Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match serde_json::to_string_pretty(self) {
Ok(json) => write!(f, "Tidal V2 API error: {}", json),
Err(_) => write!(f, "Tidal V2 API error: failed to serialize error details"),
}
}
}
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct ErrorObject {
#[serde(rename = "code", skip_serializing_if = "Option::is_none")]
pub code: Option<String>,
#[serde(rename = "detail", skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(rename = "source", skip_serializing_if = "Option::is_none")]
pub source: Option<ErrorObjectSource>,
#[serde(rename = "status", skip_serializing_if = "Option::is_none")]
pub status: Option<String>,
}
impl ErrorObject {
pub fn new() -> ErrorObject {
ErrorObject {
code: None,
detail: None,
id: None,
source: None,
status: None,
}
}
}
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct ErrorObjectSource {
#[serde(rename = "header", skip_serializing_if = "Option::is_none")]
pub header: Option<String>,
#[serde(rename = "parameter", skip_serializing_if = "Option::is_none")]
pub parameter: Option<String>,
#[serde(rename = "pointer", skip_serializing_if = "Option::is_none")]
pub pointer: Option<String>,
}
impl ErrorObjectSource {
pub fn new() -> ErrorObjectSource {
ErrorObjectSource {
header: None,
parameter: None,
pointer: None,
}
}
}