#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Http: {0}")]
Http(#[from] reqwest::Error),
#[error("Unexpected Json reply: {err}. text extract starting from err: {extract}")]
UnexpectedJson {
err: serde_json::Error,
extract: String,
},
#[error("Rate limited")]
RateLimited,
#[error("Not found")]
NotFound,
#[error("{status} from Billecta with message: `{message}`.")]
Billecta {
status: http::StatusCode,
message: String,
external_error_code: Option<String>,
request_content: Option<String>,
},
}
impl Error {
pub fn unexpected_json(err: serde_json::Error, text: &str) -> Self {
let line = match err.line() {
0 => 0,
n => n - 1,
};
let Some(offending_line) = text.lines().nth(line) else {
let extract = text.chars().take(1000).collect::<String>();
return Self::UnexpectedJson { err, extract };
};
let col = match err.column() {
0 => 0,
n => n - 1,
};
let extract = offending_line
.chars()
.skip(col)
.take(1000)
.collect::<String>();
Self::UnexpectedJson { err, extract }
}
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
pub(crate) struct BillectaErrorBody {
pub message: String,
pub external_error_code: Option<String>,
pub request_content: Option<String>,
}