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;
use std::fmt;
use serde::{de::DeserializeOwned, Deserialize};
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> {
client: Mbc,
}

View File

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