}
}
+impl ClientError {
+ fn from_response(url: &str, rsp: reqwest::blocking::Response) -> Self {
+ let rspstatus = rsp.status();
+ let message = if let Ok(text) = rsp.text() {
+ if let Ok(err) = serde_json::from_str::<ServerError>(&text) {
+ err.error
+ } else {
+ rspstatus.to_string()
+ }
+ } else {
+ rspstatus.to_string()
+ };
+
+ ClientError::UrlError(url.to_owned(), message)
+ }
+}
+
impl std::fmt::Display for ClientError {
fn fmt(
&self,
let (url, rsp) = self.api_request(Req::get("api/v2/instance"))?;
let rspstatus = rsp.status();
let inst: Instance = if !rspstatus.is_success() {
- Err(ClientError::UrlError(url, rspstatus.to_string()))
+ Err(ClientError::from_response(&url, rsp))
} else {
match serde_json::from_str(&rsp.text()?) {
Ok(ac) => Ok(ac),
let (url, rsp) = self.api_request(req)?;
let rspstatus = rsp.status();
let ac: Account = if !rspstatus.is_success() {
- Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
+ Err(ClientError::from_response(&url, rsp))
} else {
match serde_json::from_str(&rsp.text()?) {
Ok(ac) => Ok(ac),
self.api_request(Req::get(&("api/v1/polls/".to_owned() + id)))?;
let rspstatus = rsp.status();
let poll: Poll = if !rspstatus.is_success() {
- Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
+ Err(ClientError::from_response(&url, rsp))
} else {
match serde_json::from_str(&rsp.text()?) {
Ok(poll) => Ok(poll),
)?;
let rspstatus = rsp.status();
let rels: Vec<Relationship> = if !rspstatus.is_success() {
- Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
+ Err(ClientError::from_response(&url, rsp))
} else {
match serde_json::from_str(&rsp.text()?) {
Ok(ac) => Ok(ac),
self.api_request(Req::get(&("api/v1/statuses/".to_owned() + id)))?;
let rspstatus = rsp.status();
let st: Status = if !rspstatus.is_success() {
- Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
+ Err(ClientError::from_response(&url, rsp))
} else {
match serde_json::from_str(&rsp.text()?) {
Ok(st) => Ok(st),
))?;
let rspstatus = rsp.status();
let not: Notification = if !rspstatus.is_success() {
- Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
+ Err(ClientError::from_response(&url, rsp))
} else {
match serde_json::from_str(&rsp.text()?) {
Ok(st) => Ok(st),
let (url, rsp) = self.api_request(req)?;
let rspstatus = rsp.status();
let ac: Account = if !rspstatus.is_success() {
- Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
+ Err(ClientError::from_response(&url, rsp))
} else {
match serde_json::from_str(&rsp.text()?) {
Ok(ac) => Ok(ac),