1use std::str::Utf8Error;
2
3use crate::types::prelude::*;
4
5#[derive(Debug, thiserror::Error)]
7pub enum Error {
8 #[error("Server returned {code:?} -- {msg:?}")]
14 Failure {
15 code: ResponseCode,
17 resp: RawResponse,
19 msg: Option<String>,
21 },
22 #[error(transparent)]
23 Connection(#[from] crate::raw::error::Error),
27 #[error("{0}")]
29 Deserialization(String),
30 #[error("{0}")]
32 Utf8(#[from] Utf8Error),
33}
34
35impl Error {
36 pub(crate) fn failure(resp: RawResponse) -> Self {
37 Error::Failure {
38 code: resp.code(),
39 resp,
40 msg: None,
41 }
42 }
43
44 pub(crate) fn de(msg: impl AsRef<str>) -> Self {
45 Error::Deserialization(msg.as_ref().to_string())
46 }
47
48 pub(crate) fn missing_field(name: impl AsRef<str>) -> Self {
49 Error::Deserialization(format!("Missing field `{}`", name.as_ref()))
50 }
51
52 pub(crate) fn parse_error(name: impl AsRef<str>) -> Self {
53 Error::Deserialization(format!("Could not parse field `{}`", name.as_ref()))
54 }
55
56 pub(crate) fn missing_data_blocks() -> Self {
57 Error::Deserialization("Response is missing multi-line data blocks".to_string())
58 }
59
60 pub(crate) fn invalid_data_blocks(msg: impl AsRef<str>) -> Self {
61 Error::Deserialization(format!("Invalid data-block section -- {}", msg.as_ref()))
62 }
63}
64
65pub type Result<T> = std::result::Result<T, Error>;