Skip to content

Commit 4a1542c

Browse files
committed
Auto merge of #1920 - jtgeibel:rename-error-types-and-fns, r=smarnach
Begin refactoring error handling/fallback logic This PR is best reviewed as individual commits. The first commits rename some types and functions without changing anything. The final commits remove some unused logic and lay some groundwork for future refactoring. The overall intent of this PR is to more clearly document the existing behavior. In particular, the top level error types no longer include `Cargo` in the name as not all error types are intended for cargo endpoints. r? @smarnach
2 parents aaacc50 + 53e5ef2 commit 4a1542c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+310
-307
lines changed

src/background_jobs.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ use swirl::PerformError;
55
use crate::db::{DieselPool, DieselPooledConn};
66
use crate::git::Repository;
77
use crate::uploaders::Uploader;
8-
use crate::util::errors::{CargoErrToStdErr, CargoResult};
8+
use crate::util::errors::{AppErrToStdErr, AppResult};
99

1010
impl<'a> swirl::db::BorrowedConnection<'a> for DieselPool {
1111
type Connection = DieselPooledConn<'a>;
1212
}
1313

1414
impl swirl::db::DieselPool for DieselPool {
15-
type Error = CargoErrToStdErr;
15+
type Error = AppErrToStdErr;
1616

1717
fn get(&self) -> Result<swirl::db::DieselPooledConn<'_, Self>, Self::Error> {
18-
self.get().map_err(CargoErrToStdErr)
18+
self.get().map_err(AppErrToStdErr)
1919
}
2020
}
2121

@@ -59,10 +59,10 @@ impl Environment {
5959
pub fn connection(&self) -> Result<DieselPooledConn<'_>, PerformError> {
6060
self.connection_pool
6161
.get()
62-
.map_err(|e| CargoErrToStdErr(e).into())
62+
.map_err(|e| AppErrToStdErr(e).into())
6363
}
6464

65-
pub fn lock_index(&self) -> CargoResult<MutexGuard<'_, Repository>> {
65+
pub fn lock_index(&self) -> AppResult<MutexGuard<'_, Repository>> {
6666
let repo = self.index.lock().unwrap_or_else(PoisonError::into_inner);
6767
repo.reset_head()?;
6868
Ok(repo)

src/bin/enqueue-job.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use cargo_registry::util::{human, CargoError, CargoResult};
1+
use cargo_registry::util::{cargo_err, AppError, AppResult};
22
use cargo_registry::{db, env, tasks};
33
use diesel::PgConnection;
44

5-
fn main() -> CargoResult<()> {
5+
fn main() -> AppResult<()> {
66
let conn = db::connect_now()?;
77
let mut args = std::env::args().skip(1);
88
match &*args.next().unwrap_or_default() {
@@ -14,15 +14,15 @@ fn main() -> CargoResult<()> {
1414
.unwrap_or_else(|| String::from("db-dump.tar.gz"));
1515
tasks::dump_db(database_url, target_name).enqueue(&conn)
1616
}
17-
other => Err(human(&format!("Unrecognized job type `{}`", other))),
17+
other => Err(cargo_err(&format!("Unrecognized job type `{}`", other))),
1818
}
1919
}
2020

2121
/// Helper to map the `PerformError` returned by `swirl::Job::enqueue()` to a
22-
/// `CargoError`. Can be removed once `map_err()` isn't needed any more.
22+
/// `AppError`. Can be removed once `map_err()` isn't needed any more.
2323
trait Enqueue: swirl::Job {
24-
fn enqueue(self, conn: &PgConnection) -> CargoResult<()> {
25-
<Self as swirl::Job>::enqueue(self, conn).map_err(|e| CargoError::from_std_error(e))
24+
fn enqueue(self, conn: &PgConnection) -> AppResult<()> {
25+
<Self as swirl::Job>::enqueue(self, conn).map_err(|e| AppError::from_std_error(e))
2626
}
2727
}
2828

src/bin/monitor.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88

99
mod on_call;
1010

11-
use cargo_registry::{db, schema::*, util::CargoResult};
11+
use cargo_registry::{db, schema::*, util::AppResult};
1212
use diesel::prelude::*;
1313

14-
fn main() -> CargoResult<()> {
14+
fn main() -> AppResult<()> {
1515
let conn = db::connect_now()?;
1616

1717
check_stalled_background_jobs(&conn)?;
1818
check_spam_attack(&conn)?;
1919
Ok(())
2020
}
2121

22-
fn check_stalled_background_jobs(conn: &PgConnection) -> CargoResult<()> {
22+
fn check_stalled_background_jobs(conn: &PgConnection) -> AppResult<()> {
2323
use cargo_registry::schema::background_jobs::dsl::*;
2424
use diesel::dsl::*;
2525

@@ -55,7 +55,7 @@ fn check_stalled_background_jobs(conn: &PgConnection) -> CargoResult<()> {
5555
Ok(())
5656
}
5757

58-
fn check_spam_attack(conn: &PgConnection) -> CargoResult<()> {
58+
fn check_spam_attack(conn: &PgConnection) -> AppResult<()> {
5959
use cargo_registry::models::krate::canon_crate_name;
6060
use diesel::dsl::*;
6161
use diesel::sql_types::Bool;
@@ -116,7 +116,7 @@ fn check_spam_attack(conn: &PgConnection) -> CargoResult<()> {
116116
Ok(())
117117
}
118118

119-
fn log_and_trigger_event(event: on_call::Event) -> CargoResult<()> {
119+
fn log_and_trigger_event(event: on_call::Event) -> AppResult<()> {
120120
match event {
121121
on_call::Event::Trigger {
122122
ref description, ..

src/bin/on_call/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cargo_registry::util::{internal, CargoResult};
1+
use cargo_registry::util::{internal, AppResult};
22

33
use reqwest::{header, StatusCode as Status};
44

@@ -25,7 +25,7 @@ impl Event {
2525
///
2626
/// If the variant is `Trigger`, this will page whoever is on call
2727
/// (potentially waking them up at 3 AM).
28-
pub fn send(self) -> CargoResult<()> {
28+
pub fn send(self) -> AppResult<()> {
2929
let api_token = dotenv::var("PAGERDUTY_API_TOKEN")?;
3030
let service_key = dotenv::var("PAGERDUTY_INTEGRATION_KEY")?;
3131

src/boot/categories.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::{
55
db,
6-
util::errors::{internal, CargoResult, ChainError},
6+
util::errors::{internal, AppResult, ChainError},
77
};
88

99
use diesel::prelude::*;
@@ -37,7 +37,7 @@ impl Category {
3737
}
3838
}
3939

40-
fn required_string_from_toml<'a>(toml: &'a toml::value::Table, key: &str) -> CargoResult<&'a str> {
40+
fn required_string_from_toml<'a>(toml: &'a toml::value::Table, key: &str) -> AppResult<&'a str> {
4141
toml.get(key).and_then(toml::Value::as_str).chain_error(|| {
4242
internal(&format_args!(
4343
"Expected category TOML attribute '{}' to be a String",
@@ -53,7 +53,7 @@ fn optional_string_from_toml<'a>(toml: &'a toml::value::Table, key: &str) -> &'a
5353
fn categories_from_toml(
5454
categories: &toml::value::Table,
5555
parent: Option<&Category>,
56-
) -> CargoResult<Vec<Category>> {
56+
) -> AppResult<Vec<Category>> {
5757
let mut result = vec![];
5858

5959
for (slug, details) in categories {
@@ -85,12 +85,12 @@ fn categories_from_toml(
8585
Ok(result)
8686
}
8787

88-
pub fn sync(toml_str: &str) -> CargoResult<()> {
88+
pub fn sync(toml_str: &str) -> AppResult<()> {
8989
let conn = db::connect_now().unwrap();
9090
sync_with_connection(toml_str, &conn)
9191
}
9292

93-
pub fn sync_with_connection(toml_str: &str, conn: &PgConnection) -> CargoResult<()> {
93+
pub fn sync_with_connection(toml_str: &str, conn: &PgConnection) -> AppResult<()> {
9494
use crate::schema::categories::dsl::*;
9595
use diesel::dsl::all;
9696
use diesel::pg::upsert::excluded;

src/controllers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod prelude {
66
pub use conduit_router::RequestParams;
77

88
pub use crate::db::RequestTransaction;
9-
pub use crate::util::{human, CargoResult};
9+
pub use crate::util::{cargo_err, AppResult};
1010

1111
pub use crate::middleware::app::RequestApp;
1212
pub use crate::middleware::current_user::RequestUser;

src/controllers/category.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::schema::categories;
66
use crate::views::{EncodableCategory, EncodableCategoryWithSubcategories};
77

88
/// Handles the `GET /categories` route.
9-
pub fn index(req: &mut dyn Request) -> CargoResult<Response> {
9+
pub fn index(req: &mut dyn Request) -> AppResult<Response> {
1010
let conn = req.db_conn()?;
1111
let query = req.query();
1212
// FIXME: There are 69 categories, 47 top level. This isn't going to
@@ -40,7 +40,7 @@ pub fn index(req: &mut dyn Request) -> CargoResult<Response> {
4040
}
4141

4242
/// Handles the `GET /categories/:category_id` route.
43-
pub fn show(req: &mut dyn Request) -> CargoResult<Response> {
43+
pub fn show(req: &mut dyn Request) -> AppResult<Response> {
4444
let slug = &req.params()["category_id"];
4545
let conn = req.db_conn()?;
4646
let cat = Category::by_slug(slug).first::<Category>(&*conn)?;
@@ -77,7 +77,7 @@ pub fn show(req: &mut dyn Request) -> CargoResult<Response> {
7777
}
7878

7979
/// Handles the `GET /category_slugs` route.
80-
pub fn slugs(req: &mut dyn Request) -> CargoResult<Response> {
80+
pub fn slugs(req: &mut dyn Request) -> AppResult<Response> {
8181
let conn = req.db_conn()?;
8282
let slugs = categories::table
8383
.select((categories::slug, categories::slug, categories::description))

src/controllers/crate_owner_invitation.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::schema::{crate_owner_invitations, crate_owners};
55
use crate::views::{EncodableCrateOwnerInvitation, InvitationResponse};
66

77
/// Handles the `GET /me/crate_owner_invitations` route.
8-
pub fn list(req: &mut dyn Request) -> CargoResult<Response> {
8+
pub fn list(req: &mut dyn Request) -> AppResult<Response> {
99
let conn = &*req.db_conn()?;
1010
let user_id = req.user()?.id;
1111

@@ -31,14 +31,14 @@ struct OwnerInvitation {
3131
}
3232

3333
/// Handles the `PUT /me/crate_owner_invitations/:crate_id` route.
34-
pub fn handle_invite(req: &mut dyn Request) -> CargoResult<Response> {
34+
pub fn handle_invite(req: &mut dyn Request) -> AppResult<Response> {
3535
let mut body = String::new();
3636
req.body().read_to_string(&mut body)?;
3737

3838
let conn = &*req.db_conn()?;
3939

4040
let crate_invite: OwnerInvitation =
41-
serde_json::from_str(&body).map_err(|_| human("invalid json request"))?;
41+
serde_json::from_str(&body).map_err(|_| cargo_err("invalid json request"))?;
4242

4343
let crate_invite = crate_invite.crate_owner_invite;
4444

@@ -53,7 +53,7 @@ fn accept_invite(
5353
req: &dyn Request,
5454
conn: &PgConnection,
5555
crate_invite: InvitationResponse,
56-
) -> CargoResult<Response> {
56+
) -> AppResult<Response> {
5757
use diesel::{delete, insert_into};
5858

5959
let user_id = req.user()?.id;
@@ -92,7 +92,7 @@ fn decline_invite(
9292
req: &dyn Request,
9393
conn: &PgConnection,
9494
crate_invite: InvitationResponse,
95-
) -> CargoResult<Response> {
95+
) -> AppResult<Response> {
9696
use diesel::delete;
9797

9898
let user_id = req.user()?.id;

src/controllers/helpers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::util::{json_response, CargoResult};
1+
use crate::util::{json_response, AppResult};
22
use conduit::Response;
33

44
pub(crate) mod pagination;
55

66
pub(crate) use self::pagination::Paginate;
77

8-
pub fn ok_true() -> CargoResult<Response> {
8+
pub fn ok_true() -> AppResult<Response> {
99
#[derive(Serialize)]
1010
struct R {
1111
ok: bool,

src/controllers/helpers/pagination.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ pub(crate) enum Page {
1414
}
1515

1616
impl Page {
17-
fn new(params: &IndexMap<String, String>) -> CargoResult<Self> {
17+
fn new(params: &IndexMap<String, String>) -> AppResult<Self> {
1818
if let Some(s) = params.get("page") {
1919
let numeric_page = s.parse()?;
2020
if numeric_page < 1 {
21-
return Err(human(&format_args!(
21+
return Err(cargo_err(&format_args!(
2222
"page indexing starts from 1, page {} is invalid",
2323
numeric_page,
2424
)));
@@ -38,7 +38,7 @@ pub(crate) struct PaginationOptions {
3838
}
3939

4040
impl PaginationOptions {
41-
pub(crate) fn new(params: &IndexMap<String, String>) -> CargoResult<Self> {
41+
pub(crate) fn new(params: &IndexMap<String, String>) -> AppResult<Self> {
4242
const DEFAULT_PER_PAGE: u32 = 10;
4343
const MAX_PER_PAGE: u32 = 100;
4444

@@ -48,7 +48,7 @@ impl PaginationOptions {
4848
.unwrap_or(Ok(DEFAULT_PER_PAGE))?;
4949

5050
if per_page > MAX_PER_PAGE {
51-
return Err(human(&format_args!(
51+
return Err(cargo_err(&format_args!(
5252
"cannot request more than {} items",
5353
MAX_PER_PAGE,
5454
)));
@@ -70,7 +70,7 @@ impl PaginationOptions {
7070
}
7171

7272
pub(crate) trait Paginate: Sized {
73-
fn paginate(self, params: &IndexMap<String, String>) -> CargoResult<PaginatedQuery<Self>> {
73+
fn paginate(self, params: &IndexMap<String, String>) -> AppResult<PaginatedQuery<Self>> {
7474
Ok(PaginatedQuery {
7575
query: self,
7676
options: PaginationOptions::new(params)?,

src/controllers/keyword.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::models::Keyword;
55
use crate::views::EncodableKeyword;
66

77
/// Handles the `GET /keywords` route.
8-
pub fn index(req: &mut dyn Request) -> CargoResult<Response> {
8+
pub fn index(req: &mut dyn Request) -> AppResult<Response> {
99
use crate::schema::keywords;
1010

1111
let conn = req.db_conn()?;
@@ -41,7 +41,7 @@ pub fn index(req: &mut dyn Request) -> CargoResult<Response> {
4141
}
4242

4343
/// Handles the `GET /keywords/:keyword_id` route.
44-
pub fn show(req: &mut dyn Request) -> CargoResult<Response> {
44+
pub fn show(req: &mut dyn Request) -> AppResult<Response> {
4545
let name = &req.params()["keyword_id"];
4646
let conn = req.db_conn()?;
4747

src/controllers/krate/downloads.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::views::EncodableVersionDownload;
1414
use crate::models::krate::to_char;
1515

1616
/// Handles the `GET /crates/:crate_id/downloads` route.
17-
pub fn downloads(req: &mut dyn Request) -> CargoResult<Response> {
17+
pub fn downloads(req: &mut dyn Request) -> AppResult<Response> {
1818
use diesel::dsl::*;
1919
use diesel::sql_types::BigInt;
2020

src/controllers/krate/follow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::db::DieselPooledConn;
77
use crate::models::{Crate, Follow};
88
use crate::schema::*;
99

10-
fn follow_target(req: &dyn Request, conn: &DieselPooledConn<'_>) -> CargoResult<Follow> {
10+
fn follow_target(req: &dyn Request, conn: &DieselPooledConn<'_>) -> AppResult<Follow> {
1111
let user = req.user()?;
1212
let crate_name = &req.params()["crate_id"];
1313
let crate_id = Crate::by_name(crate_name)
@@ -20,7 +20,7 @@ fn follow_target(req: &dyn Request, conn: &DieselPooledConn<'_>) -> CargoResult<
2020
}
2121

2222
/// Handles the `PUT /crates/:crate_id/follow` route.
23-
pub fn follow(req: &mut dyn Request) -> CargoResult<Response> {
23+
pub fn follow(req: &mut dyn Request) -> AppResult<Response> {
2424
let conn = req.db_conn()?;
2525
let follow = follow_target(req, &conn)?;
2626
diesel::insert_into(follows::table)
@@ -32,7 +32,7 @@ pub fn follow(req: &mut dyn Request) -> CargoResult<Response> {
3232
}
3333

3434
/// Handles the `DELETE /crates/:crate_id/follow` route.
35-
pub fn unfollow(req: &mut dyn Request) -> CargoResult<Response> {
35+
pub fn unfollow(req: &mut dyn Request) -> AppResult<Response> {
3636
let conn = req.db_conn()?;
3737
let follow = follow_target(req, &conn)?;
3838
diesel::delete(&follow).execute(&*conn)?;
@@ -41,7 +41,7 @@ pub fn unfollow(req: &mut dyn Request) -> CargoResult<Response> {
4141
}
4242

4343
/// Handles the `GET /crates/:crate_id/following` route.
44-
pub fn following(req: &mut dyn Request) -> CargoResult<Response> {
44+
pub fn following(req: &mut dyn Request) -> AppResult<Response> {
4545
use diesel::dsl::exists;
4646

4747
let conn = req.db_conn()?;

0 commit comments

Comments
 (0)