Skip to content

Commit 82e8f1b

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

File tree

1 file changed

+28
-24
lines changed
  • chloria-backend/chloria-job/src/infrastructure

1 file changed

+28
-24
lines changed

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::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

@@ -102,30 +102,34 @@ impl NewsFetcher for NewsdataClient {
102102
} {
103103
break;
104104
}
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));
105+
match self.fetch_page(page).await {
106+
Ok((total_results, page_articles, next_page)) => {
107+
info!(
108+
"page_index={}, total_results={}, page_articles.len={}, next_page={:?}",
109+
page_index,
110+
total_results,
111+
page_articles.len(),
112+
next_page,
113+
);
114+
remaining_results_num = Some(
115+
match remaining_results_num {
116+
Some(remaining_results_num) => remaining_results_num,
117+
None => total_results as i32,
118+
} - page_articles.len() as i32,
119+
);
120+
for article in page_articles {
121+
output.push(handler(article));
122+
}
123+
match next_page {
124+
Some(next_page) => page = Some(next_page),
125+
None => break,
126+
};
127+
page_index += 1;
128+
}
129+
Err(error) => {
130+
error!("page_index={}, error={}", page_index, error);
131+
break;
121132
}
122-
match next_page {
123-
Some(next_page) => page = Some(next_page),
124-
None => break,
125-
};
126-
page_index += 1;
127-
} else {
128-
break;
129133
}
130134
}
131135
output

0 commit comments

Comments
 (0)