use std::str::Utf8Error;
use crate::types::prelude::*;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Server returned {code:?} -- {msg:?}")]
Failure {
code: ResponseCode,
resp: RawResponse,
msg: Option<String>,
},
#[error(transparent)]
Connection(#[from] crate::raw::error::Error),
#[error("{0}")]
Deserialization(String),
#[error("{0}")]
Utf8(#[from] Utf8Error),
}
impl Error {
pub(crate) fn failure(resp: RawResponse) -> Self {
Error::Failure {
code: resp.code(),
resp,
msg: None,
}
}
pub(crate) fn de(msg: impl AsRef<str>) -> Self {
Error::Deserialization(msg.as_ref().to_string())
}
pub(crate) fn missing_field(name: impl AsRef<str>) -> Self {
Error::Deserialization(format!("Missing field `{}`", name.as_ref()))
}
pub(crate) fn parse_error(name: impl AsRef<str>) -> Self {
Error::Deserialization(format!("Could not parse field `{}`", name.as_ref()))
}
pub(crate) fn missing_data_blocks() -> Self {
Error::Deserialization("Response is missing multi-line data blocks".to_string())
}
pub(crate) fn invalid_data_blocks(msg: impl AsRef<str>) -> Self {
Error::Deserialization(format!("Invalid data-block section -- {}", msg.as_ref()))
}
}
pub type Result<T> = std::result::Result<T, Error>;