Skip to content

Commit

Permalink
feat: improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shonya3 committed Aug 17, 2023
1 parent e00ace3 commit 1bd571b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
1 change: 0 additions & 1 deletion divi/p.json

This file was deleted.

8 changes: 7 additions & 1 deletion divi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ use std::fmt::Display;

use serde::Serialize;

use crate::league::TradeLeague;

#[derive(Debug)]
pub enum Error {
HttpError(reqwest::Error),
SerdeError(serde_json::Error),
MissingHeaders,
NoPricesForLeagueOnNinja(TradeLeague),
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::HttpError(err) => err.fmt(f),
Error::SerdeError(err) => err.fmt(f),
Error::MissingHeaders => write!(f, "File should contain headers: name, amount"),
Error::MissingHeaders => write!(f, "File should contain headers: name, amount."),
Error::NoPricesForLeagueOnNinja(league) => {
write!(f, "Prices for {} league do not exist on poe.ninja.", league)
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion divi/src/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ impl Prices {
let json = client.get(url).send().await?.text().await?;
// std::fs::write("ninja.json", &json).unwrap();
let data = serde_json::from_str::<PriceData>(&json).unwrap();
dbg!(&data);
if data.lines.len() == 0 {
return Err(Error::NoPricesForLeagueOnNinja(league.to_owned()));
}
Ok(Prices::from(data.lines))
}
}
Expand Down
2 changes: 1 addition & 1 deletion divi/src/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl DivinationCardsSample {
/// Consumes Prices structure to set prices for Cards
fn from_prices(prices: Option<Prices>) -> Self {
DivinationCardsSample {
cards: prices.unwrap_or_default().into(),
cards: Cards::from(prices.unwrap_or_default()),
..Default::default()
}
}
Expand Down
35 changes: 18 additions & 17 deletions packages/app/src-tauri/lib/src/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl AppCardPrices {
}
}

#[instrument(skip(self, window))]
// #[instrument(skip(self, window))]
pub async fn get_or_update_or_default(
&mut self,
league: &TradeLeague,
Expand Down Expand Up @@ -71,7 +71,7 @@ impl AppCardPrices {
false => {
debug!("File is too old. Return default Prices with warning toast");
self.send_default_prices_with_toast_warning(
league, window,
&err, league, window,
)
}
}
Expand All @@ -86,7 +86,7 @@ impl AppCardPrices {
Ok(prices) => prices,
Err(err) => {
debug!("Unable to fetch prices: {err}. Return default Prices with warning toast");
self.send_default_prices_with_toast_warning(league, window)
self.send_default_prices_with_toast_warning(&err, league, window)
}
}
}
Expand All @@ -95,6 +95,21 @@ impl AppCardPrices {
}
}

#[instrument(skip(self, window))]
fn send_default_prices_with_toast_warning(
&self,
err: &Error,
league: &TradeLeague,
window: &Window,
) -> Prices {
Event::Toast {
variant: ToastVariant::Warning,
message: format!("{err} Unable to load prices for league {league}. Skip price-dependant calculations."),
}
.emit(&window);
Prices::default()
}

#[instrument(skip(self))]
fn read_from_file_update_and_return(&mut self, league: &TradeLeague) -> Prices {
let prices = self.read_from_file(league).unwrap();
Expand Down Expand Up @@ -146,20 +161,6 @@ impl AppCardPrices {
}
}

#[instrument(skip(self, window))]
fn send_default_prices_with_toast_warning(
&self,
league: &TradeLeague,
window: &Window,
) -> Prices {
Event::Toast {
variant: ToastVariant::Warning,
message: String::from("Unable to load prices. Skip price-dependant calculations."),
}
.emit(&window);
Prices::default()
}

#[instrument(skip(self))]
fn file_is_up_to_date(&self, league: &TradeLeague) -> bool {
match self.file_days_old(league) {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export type Order = 'asc' | 'desc' | 'unordered';
export const leagues = [
'Ancestor',
'Standard',
'Hardcore Ancestor',
'Ancestor-HC',
'Hardcore',
'SSF Standard',
'SSF Hardcore',
Expand Down

0 comments on commit 1bd571b

Please sign in to comment.