HTTP client error handling

This commit is contained in:
Wojciech Kozlowski 2024-08-26 17:27:58 +02:00
parent 3c29fda119
commit e9ca6ccdfe
2 changed files with 26 additions and 9 deletions

View File

@ -2,6 +2,8 @@
pub mod client; pub mod client;
use std::fmt;
use serde::{de::DeserializeOwned, Deserialize}; use serde::{de::DeserializeOwned, Deserialize};
use url::form_urlencoded; use url::form_urlencoded;
@ -42,6 +44,15 @@ impl From<ClientError> for Error {
} }
} }
impl fmt::Display for ClientError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ClientError::Client(s) => write!(f, "the API client failed: {s}"),
ClientError::Status(u) => write!(f, "the API client failed with status: {u}"),
}
}
}
pub struct MusicBrainzApi<Mbc> { pub struct MusicBrainzApi<Mbc> {
client: Mbc, client: Mbc,
} }

View File

@ -12,8 +12,9 @@ use crate::tui::{
selection::{Delta, ListSelection}, selection::{Delta, ListSelection},
AppPublic, AppState, IAppInteractBrowse, AppPublic, AppState, IAppInteractBrowse,
}, },
lib::IMusicHoard, MUSICHOARD_TUI_HTTP_USER_AGENT, lib::IMusicHoard,
}; MUSICHOARD_TUI_HTTP_USER_AGENT,
};
pub struct AppBrowse; pub struct AppBrowse;
@ -90,8 +91,13 @@ impl<MH: IMusicHoard> IAppInteractBrowse for AppMachine<MH, AppBrowse> {
} }
fn fetch_musicbrainz(self) -> Self::APP { fn fetch_musicbrainz(self) -> Self::APP {
let client = MusicBrainzApiClient::new(MUSICHOARD_TUI_HTTP_USER_AGENT) let client = match MusicBrainzApiClient::new(MUSICHOARD_TUI_HTTP_USER_AGENT) {
.expect("failed to create API client"); Ok(client) => client,
Err(err) => {
return AppMachine::error(self.inner, format!("cannot fetch: {}", err.to_string()))
.into()
}
};
let mut api = MusicBrainzApi::new(client); let mut api = MusicBrainzApi::new(client);
let coll = self.inner.music_hoard.get_collection(); let coll = self.inner.music_hoard.get_collection();