use serde::Deserialize;
use std::convert::From;
use std::fmt;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[error("API error code {0}: {1}")]
RestApi(u64, String),
#[error("Unable read response")]
ReadResponse(Box<anyhow::Error>),
#[error("Invalid value: {0}")]
WsInvalidValue(String),
#[error(transparent)]
WsApiParse(serde_json::Error),
}
#[derive(Deserialize, Debug)]
struct ApiErrorDetail {
code: u64,
message: String,
}
impl fmt::Display for ApiErrorDetail {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MaiCoin MAX Error {}: {}", self.code, self.message)
}
}
#[derive(Deserialize, Debug)]
pub(crate) struct ApiErrorWrapper {
error: ApiErrorDetail,
}
impl From<ApiErrorWrapper> for Error {
fn from(err: ApiErrorWrapper) -> Self {
Error::RestApi(err.error.code, err.error.message)
}
}