use df_hyperliquid_core::client::TransportError;
use df_hyperliquid_core::utils::Json;
use crate::utils::wire::BadWire;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum RelayerError {
#[error(transparent)]
Transport(#[from] TransportError),
#[error("the relayer refused ({status}{}): {}", code.as_deref().map(|code| format!(", {code}")).unwrap_or_default(), message.as_deref().unwrap_or("no reason given"))]
Refused { status: u16, code: Option<String>, message: Option<String> },
#[error("the relayer's answer did not read: {0}")]
Unreadable(#[from] BadWire),
#[error("the relayer accepted a different intent ({found}) than the one sent ({sent})")]
OtherIntent { sent: String, found: String },
}
pub(super) fn refusal(status: u16, body: &[u8]) -> RelayerError {
let json = std::str::from_utf8(body).ok().and_then(|text| Json::parse(text).ok());
let text = |key: &str| json.as_ref().and_then(|json| json.get(key)).and_then(Json::as_str).map(str::to_owned);
RelayerError::Refused { status, code: text("error").or_else(|| text("code")), message: text("message") }
}
#[cfg(test)]
#[path = "tests/error.rs"]
mod tests;