Skip to content

Commit cd64816

Browse files
committed
bugfix: improved error handling
1 parent 5736ede commit cd64816

File tree

2 files changed

+78
-108
lines changed

2 files changed

+78
-108
lines changed

server/src/contract_pull.rs

Lines changed: 58 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{
33
time::Duration,
44
};
55

6+
use anyhow::Context;
67
use chrono::DateTime;
78
use rocket::fairing::AdHoc;
89
use rocket_db_pools::Database;
@@ -12,7 +13,6 @@ use sqlx::{Postgres, Transaction};
1213
use crate::db::DB;
1314

1415
async fn fetch_and_store_users(
15-
telegram: &Arc<TelegramSubscriber>,
1616
near_client: &NearClient,
1717
tx: &mut Transaction<'static, Postgres>,
1818
) -> anyhow::Result<()> {
@@ -23,61 +23,61 @@ async fn fetch_and_store_users(
2323
.into_iter()
2424
.map(|e| e.time_string(timestamp as u64))
2525
.collect();
26-
let users = near_client.users(periods).await?;
26+
let users = near_client
27+
.users(periods)
28+
.await
29+
.context("Failed to fetch users")?;
2730

2831
for user in users {
29-
let user_id = match DB::upsert_user(tx, user.id, &user.name, user.percentage_bonus).await {
30-
Ok(id) => id,
31-
Err(e) => {
32-
crate::error(
33-
telegram,
34-
&format!("Failed to upsert user ({}): {:#?}", user.name, e),
35-
);
36-
continue;
37-
}
38-
};
32+
let user_id = DB::upsert_user(tx, user.id, &user.name, user.percentage_bonus)
33+
.await
34+
.with_context(|| format!("Failed to upsert user with id: {}", user.id))?;
3935
for (period, data) in user.period_data {
40-
if let Err(e) = DB::upsert_user_period_data(tx, period, &data, user_id).await {
41-
crate::error(
42-
telegram,
43-
&format!(
44-
"Failed to upsert user ({}) period data: {:#?}",
45-
user.name, e
46-
),
47-
);
48-
}
36+
DB::upsert_user_period_data(tx, period, &data, user_id)
37+
.await
38+
.with_context(|| {
39+
format!("Failed to upsert period data for user id: {}", user_id)
40+
})?;
4941
}
5042
for (streak_id, streak_data) in user.streaks {
51-
if let Err(e) =
52-
DB::upsert_streak_user_data(tx, &streak_data, streak_id as i32, user_id).await
53-
{
54-
crate::error(
55-
telegram,
56-
&format!(
57-
"Failed to upsert user ({}) streak data: {:#?}",
58-
user.name, e
59-
),
60-
);
61-
}
43+
DB::upsert_streak_user_data(tx, &streak_data, streak_id as i32, user_id)
44+
.await
45+
.with_context(|| {
46+
format!(
47+
"Failed to upsert streak data for user id: {} and streak id: {}",
48+
user_id, streak_id
49+
)
50+
})?;
6251
}
6352
}
6453

6554
Ok(())
6655
}
6756

6857
async fn fetch_and_store_prs(
69-
telegram: &Arc<TelegramSubscriber>,
7058
near_client: &NearClient,
7159
tx: &mut Transaction<'static, Postgres>,
7260
) -> anyhow::Result<()> {
73-
let prs = near_client.prs().await?;
61+
let prs = near_client
62+
.prs()
63+
.await
64+
.context("Failed to fetch PRs from near_client")?;
65+
66+
DB::clear_prs(tx)
67+
.await
68+
.context("Failed to clear existing PRs from the database")?;
7469

75-
DB::clear_prs(tx).await?;
7670
for (pr, executed) in prs {
77-
let organization_id = DB::upsert_organization(tx, &pr.organization).await?;
78-
let repo_id = DB::upsert_repo(tx, organization_id, &pr.repo).await?;
79-
let author_id = DB::get_user_id(tx, &pr.author).await?;
80-
if let Err(e) = DB::upsert_pull_request(
71+
let organization_id = DB::upsert_organization(tx, &pr.organization)
72+
.await
73+
.context("Failed on upserting organization")?;
74+
let repo_id = DB::upsert_repo(tx, organization_id, &pr.repo)
75+
.await
76+
.context("Failed on upserting repo")?;
77+
let author_id = DB::get_user_id(tx, &pr.author)
78+
.await
79+
.context("Failed on getting user id")?;
80+
DB::upsert_pull_request(
8181
tx,
8282
repo_id,
8383
pr.number as i32,
@@ -93,69 +93,44 @@ async fn fetch_and_store_prs(
9393
executed,
9494
)
9595
.await
96-
{
97-
crate::error(
98-
telegram,
99-
&format!(
100-
"Failed to upsert PR ({}/{}/pull/{}): {:#?}",
101-
pr.organization, pr.repo, pr.number, e
102-
),
103-
);
104-
}
96+
.context("Failed on upserting PR")?;
10597
}
10698

10799
Ok(())
108100
}
109101

110102
async fn fetch_and_store_repos(
111-
telegram: &Arc<TelegramSubscriber>,
112103
near_client: &NearClient,
113104
tx: &mut Transaction<'static, Postgres>,
114105
) -> anyhow::Result<()> {
115106
let organizations = near_client.repos().await?;
116107
for org in organizations {
117-
let organization_id = match DB::upsert_organization(tx, &org.organization).await {
118-
Ok(id) => id,
119-
Err(e) => {
120-
crate::error(
121-
telegram,
122-
&format!(
123-
"Failed to upsert organization ({}): {:#?}",
124-
org.organization, e
125-
),
126-
);
127-
continue;
128-
}
129-
};
108+
let organization_id = DB::upsert_organization(tx, &org.organization)
109+
.await
110+
.context("Failed on upserting organization")?;
130111
for repo in org.repos {
131-
if let Err(e) = DB::upsert_repo(tx, organization_id, &repo).await {
132-
crate::error(
133-
telegram,
134-
&format!(
135-
"Failed to upsert repo ({}/{}): {:#?}",
136-
org.organization, repo, e
137-
),
138-
);
139-
}
112+
DB::upsert_repo(tx, organization_id, &repo)
113+
.await
114+
.context("Failed on upserting repo")?;
140115
}
141116
}
142117

143118
Ok(())
144119
}
145120

146121
// TODO: more efficient way to fetch only updated data
147-
async fn fetch_and_store_all_data(
148-
telegram: &Arc<TelegramSubscriber>,
149-
near_client: &NearClient,
150-
db: &DB,
151-
) -> anyhow::Result<()> {
122+
async fn fetch_and_store_all_data(near_client: &NearClient, db: &DB) -> anyhow::Result<()> {
152123
let mut tx = db.begin().await?;
153124

154-
fetch_and_store_users(telegram, near_client, &mut tx).await?;
155-
156-
fetch_and_store_repos(telegram, near_client, &mut tx).await?;
157-
// It matters that we fetch users first, because we need to know their IDs
158-
fetch_and_store_prs(telegram, near_client, &mut tx).await?;
125+
fetch_and_store_users(near_client, &mut tx)
126+
.await
127+
.context("Failed to fetch and store users")?;
128+
fetch_and_store_repos(near_client, &mut tx)
129+
.await
130+
.context("Failed to fetch and store repositories")?;
131+
fetch_and_store_prs(near_client, &mut tx)
132+
.await
133+
.context("Failed to fetch and store pull requests")?;
159134

160135
tx.commit().await?;
161136
Ok(())
@@ -180,11 +155,8 @@ pub fn stage(client: NearClient, sleep_duration: Duration, atomic_bool: Arc<Atom
180155
interval.tick().await;
181156

182157
// Execute a query of some kind
183-
if let Err(e) = fetch_and_store_all_data(&telegram, &near_client, &db).await {
184-
crate::error(
185-
&telegram,
186-
&format!("Failed to fetch and store data: {:#?}", e),
187-
);
158+
if let Err(e) = fetch_and_store_all_data(&near_client, &db).await {
159+
crate::error(&telegram, &e.to_string());
188160
}
189161
}
190162
});

server/src/github_pull.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ use std::{
33
time::Duration,
44
};
55

6+
use anyhow::Context;
67
use octocrab::{models::pulls::PullRequest, Octocrab};
78
use rocket::fairing::AdHoc;
89
use rocket_db_pools::Database;
910
use shared::telegram::TelegramSubscriber;
1011
use sqlx::{Postgres, Transaction};
1112
use tracing::instrument;
1213

13-
use crate::db::DB;
14+
use crate::{db::DB, error};
1415

1516
struct RepoMetadata {
1617
stars: u32,
@@ -100,7 +101,7 @@ async fn fetch_repos_metadata(
100101
github: &GithubClient,
101102
tx: &mut Transaction<'static, Postgres>,
102103
) -> anyhow::Result<()> {
103-
let repos = DB::get_repos(tx).await?;
104+
let repos = DB::get_repos(tx).await.context("Failed to ger repops")?;
104105
for repo in repos {
105106
let metadata = match github.repo_metadata(&repo.organization, &repo.repo).await {
106107
Ok(metadata) => metadata,
@@ -115,7 +116,7 @@ async fn fetch_repos_metadata(
115116
continue;
116117
}
117118
};
118-
if let Err(e) = DB::update_repo_metadata(
119+
DB::update_repo_metadata(
119120
tx,
120121
repo.repo_id,
121122
metadata.stars,
@@ -124,15 +125,7 @@ async fn fetch_repos_metadata(
124125
metadata.primary_language,
125126
)
126127
.await
127-
{
128-
crate::error(
129-
telegram,
130-
&format!(
131-
"Failed to update repo metadata for {}/{}: {:#?}",
132-
&repo.organization, &repo.repo, e
133-
),
134-
);
135-
}
128+
.context("Failed to update repo metadata")?;
136129
}
137130
Ok(())
138131
}
@@ -143,7 +136,7 @@ async fn fetch_missing_user_organization_metadata(
143136
github: &GithubClient,
144137
tx: &mut Transaction<'static, Postgres>,
145138
) -> anyhow::Result<()> {
146-
let users = DB::get_users(tx).await?;
139+
let users = DB::get_users(tx).await.unwrap_or_default();
147140
for user in users {
148141
if user.full_name.is_some() {
149142
// TODO: add user entry to sync cache
@@ -161,11 +154,13 @@ async fn fetch_missing_user_organization_metadata(
161154
}
162155
};
163156
if let Some(full_name) = &profile.name {
164-
DB::update_user_full_name(tx, &user.login, full_name).await?;
157+
DB::update_user_full_name(tx, &user.login, full_name)
158+
.await
159+
.context("Failed to update user full name")?;
165160
}
166161
}
167162

168-
let orgs = DB::get_organizations(tx).await?;
163+
let orgs = DB::get_organizations(tx).await.unwrap_or_default();
169164
for org in orgs {
170165
if org.full_name.is_some() {
171166
continue;
@@ -185,7 +180,9 @@ async fn fetch_missing_user_organization_metadata(
185180
}
186181
};
187182
if let Some(full_name) = &profile.name {
188-
DB::update_organization_full_name(tx, &org.login, full_name).await?;
183+
DB::update_organization_full_name(tx, &org.login, full_name)
184+
.await
185+
.context("Failed to update organization full name")?;
189186
}
190187
}
191188
Ok(())
@@ -198,8 +195,12 @@ pub async fn fetch_github_data(
198195
) -> anyhow::Result<()> {
199196
let mut tx = db.begin().await?;
200197

201-
fetch_repos_metadata(telegram, github, &mut tx).await?;
202-
fetch_missing_user_organization_metadata(telegram, github, &mut tx).await?;
198+
fetch_repos_metadata(telegram, github, &mut tx)
199+
.await
200+
.context("Failure on fetching and updating repos")?;
201+
fetch_missing_user_organization_metadata(telegram, github, &mut tx)
202+
.await
203+
.context("Failed ton fetching and updating user/org metadata")?;
203204

204205
tx.commit().await?;
205206

@@ -239,10 +240,7 @@ pub fn stage(
239240
if let Err(e) =
240241
fetch_github_data(&telegram, &github_client, &db).await
241242
{
242-
rocket::error!(
243-
"Failed to fetch and store github data: {:#?}",
244-
e
245-
);
243+
error(&telegram, &e.to_string());
246244
}
247245
}
248246
});

0 commit comments

Comments
 (0)