Skip to content

Commit a705914

Browse files
committed
Fix: Allow negative values for remaining results. Show errors if fetching fails.
1 parent 1bd2537 commit a705914

File tree

1 file changed

+31
-26
lines changed
  • chloria-backend/chloria-job/src/infrastructure

1 file changed

+31
-26
lines changed

chloria-backend/chloria-job/src/infrastructure/newsdata.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use anyhow::Result;
1+
use anyhow::{Context, Result};
22
use async_trait::async_trait;
33
use chrono::NaiveDateTime;
44
use chrono_tz::Tz;
5-
use log::info;
5+
use log::{error, info};
66
use reqwest::Client;
77
use serde::Deserialize;
88

@@ -51,7 +51,8 @@ impl NewsdataClient {
5151
url.push_str(&format!("&page={page}"));
5252
}
5353
let response_text = Client::new().get(url).send().await?.text().await?;
54-
let news_response: NewsResponse = serde_json::from_str(&response_text)?;
54+
let news_response: NewsResponse = serde_json::from_str(&response_text)
55+
.with_context(|| format!("Deserializing the response text: {}", response_text))?;
5556
let mut articles = vec![];
5657
for result in news_response.results.into_iter() {
5758
let published_time = if let (Some(pub_date), Some(pub_date_tz)) = (result.pub_date, result.pub_date_tz) {
@@ -102,30 +103,34 @@ impl NewsFetcher for NewsdataClient {
102103
} {
103104
break;
104105
}
105-
if let Ok((total_results, page_articles, next_page)) = self.fetch_page(page).await {
106-
info!(
107-
"total_results={}, page_index={}, page_articles.len={}, next_page={:?}",
108-
total_results,
109-
page_index,
110-
page_articles.len(),
111-
next_page,
112-
);
113-
remaining_results_num = Some(
114-
match remaining_results_num {
115-
Some(remaining_results_num) => remaining_results_num,
116-
None => total_results,
117-
} - page_articles.len() as u16,
118-
);
119-
for article in page_articles {
120-
output.push(handler(article));
106+
match self.fetch_page(page).await {
107+
Ok((total_results, page_articles, next_page)) => {
108+
info!(
109+
"page_index={}, total_results={}, page_articles.len={}, next_page={:?}",
110+
page_index,
111+
total_results,
112+
page_articles.len(),
113+
next_page,
114+
);
115+
remaining_results_num = Some(
116+
match remaining_results_num {
117+
Some(remaining_results_num) => remaining_results_num,
118+
None => total_results as i32,
119+
} - page_articles.len() as i32,
120+
);
121+
for article in page_articles {
122+
output.push(handler(article));
123+
}
124+
match next_page {
125+
Some(next_page) => page = Some(next_page),
126+
None => break,
127+
};
128+
page_index += 1;
129+
}
130+
Err(error) => {
131+
error!("page_index={}, error={}", page_index, error);
132+
break;
121133
}
122-
match next_page {
123-
Some(next_page) => page = Some(next_page),
124-
None => break,
125-
};
126-
page_index += 1;
127-
} else {
128-
break;
129134
}
130135
}
131136
output

0 commit comments

Comments
 (0)