Skip to content

Commit 205fd2d

Browse files
Merge pull request #572 from Eh2406/try-to-q-mark
Replace try with ?
2 parents fd7fb05 + 4c908cc commit 205fd2d

20 files changed

+616
-621
lines changed

src/bin/fill-in-user-id.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ fn update(app: &App, tx: &postgres::transaction::Transaction) {
6767
println!("attempt: {}/{}", id, login);
6868
let res = (|| -> CargoResult<()> {
6969
let url = format!("/users/{}", login);
70-
let (handle, resp) = try!(http::github(app, &url, &token));
71-
let ghuser: GithubUser = try!(http::parse_github_response(handle, resp));
70+
let (handle, resp) = http::github(app, &url, &token)?;
71+
let ghuser: GithubUser = http::parse_github_response(handle, resp)?;
7272
if let Some(ref avatar) = avatar {
7373
if !avatar.contains(&ghuser.id.to_string()) {
7474
return Err(human(format!("avatar: {}", avatar)))
7575
}
7676
}
7777
if ghuser.login == login {
78-
try!(tx.execute("UPDATE users SET gh_id = $1 WHERE id = $2",
79-
&[&ghuser.id, &id]));
78+
tx.execute("UPDATE users SET gh_id = $1 WHERE id = $2",
79+
&[&ghuser.id, &id])?;
8080
Ok(())
8181
} else {
8282
Err(human(format!("different login: {}", ghuser.login)))

src/bin/migrate.rs

Lines changed: 116 additions & 116 deletions
Large diffs are not rendered by default.

src/bin/populate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ fn update(tx: &postgres::transaction::Transaction) -> postgres::Result<()> {
3838
for day in 0..90 {
3939
let moment = now + Duration::days(-day);
4040
dls += rng.gen_range(-100, 100);
41-
try!(tx.execute("INSERT INTO version_downloads \
41+
tx.execute("INSERT INTO version_downloads \
4242
(version_id, downloads, date) \
4343
VALUES ($1, $2, $3)",
44-
&[&id, &dls, &moment]));
44+
&[&id, &dls, &moment])?;
4545
}
4646
}
4747
Ok(())

src/bin/update-downloads.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ fn update(conn: &postgres::GenericConnection) -> postgres::Result<()> {
3636
loop {
3737
// FIXME(rust-lang/rust#27401): weird declaration to make sure this
3838
// variable gets dropped.
39-
let tx; tx = try!(conn.transaction());
39+
let tx; tx = conn.transaction()?;
4040
{
41-
let stmt = try!(tx.prepare("SELECT * FROM version_downloads \
41+
let stmt = tx.prepare("SELECT * FROM version_downloads \
4242
WHERE processed = FALSE AND id > $1
4343
ORDER BY id ASC
44-
LIMIT $2"));
45-
let mut rows = try!(stmt.query(&[&max, &LIMIT]));
46-
match try!(collect(&tx, &mut rows)) {
44+
LIMIT $2")?;
45+
let mut rows = stmt.query(&[&max, &LIMIT])?;
46+
match collect(&tx, &mut rows)? {
4747
None => break,
4848
Some(m) => max = m,
4949
}
5050
}
5151
tx.set_commit();
52-
try!(tx.finish());
52+
tx.finish()?;
5353
}
5454
Ok(())
5555
}
@@ -87,10 +87,10 @@ fn collect(tx: &postgres::transaction::Transaction,
8787

8888
// Flag this row as having been processed if we're passed the cutoff,
8989
// and unconditionally increment the number of counted downloads.
90-
try!(tx.execute("UPDATE version_downloads
90+
tx.execute("UPDATE version_downloads
9191
SET processed = $2, counted = counted + $3
9292
WHERE id = $1",
93-
&[id, &(download.date < cutoff), &amt]));
93+
&[id, &(download.date < cutoff), &amt])?;
9494
println!("{}\n{}", time::at(download.date).rfc822(),
9595
time::at(cutoff).rfc822());
9696
total += amt as i64;
@@ -102,31 +102,31 @@ fn collect(tx: &postgres::transaction::Transaction,
102102
let crate_id = Version::find(tx, download.version_id).unwrap().crate_id;
103103

104104
// Update the total number of version downloads
105-
try!(tx.execute("UPDATE versions
105+
tx.execute("UPDATE versions
106106
SET downloads = downloads + $1
107107
WHERE id = $2",
108-
&[&amt, &download.version_id]));
108+
&[&amt, &download.version_id])?;
109109
// Update the total number of crate downloads
110-
try!(tx.execute("UPDATE crates SET downloads = downloads + $1
111-
WHERE id = $2", &[&amt, &crate_id]));
110+
tx.execute("UPDATE crates SET downloads = downloads + $1
111+
WHERE id = $2", &[&amt, &crate_id])?;
112112

113113
// Update the total number of crate downloads for today
114-
let cnt = try!(tx.execute("UPDATE crate_downloads
114+
let cnt = tx.execute("UPDATE crate_downloads
115115
SET downloads = downloads + $2
116116
WHERE crate_id = $1 AND date = date($3)",
117-
&[&crate_id, &amt, &download.date]));
117+
&[&crate_id, &amt, &download.date])?;
118118
if cnt == 0 {
119-
try!(tx.execute("INSERT INTO crate_downloads
119+
tx.execute("INSERT INTO crate_downloads
120120
(crate_id, downloads, date)
121121
VALUES ($1, $2, $3)",
122-
&[&crate_id, &amt, &download.date]));
122+
&[&crate_id, &amt, &download.date])?;
123123
}
124124
}
125125

126126
// After everything else is done, update the global counter of total
127127
// downloads.
128-
try!(tx.execute("UPDATE metadata SET total_downloads = total_downloads + $1",
129-
&[&total]));
128+
tx.execute("UPDATE metadata SET total_downloads = total_downloads + $1",
129+
&[&total])?;
130130

131131
Ok(Some(max))
132132
}

src/category.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ pub struct EncodableCategoryWithSubcategories {
4545
impl Category {
4646
pub fn find_by_category(conn: &GenericConnection, name: &str)
4747
-> CargoResult<Category> {
48-
let stmt = try!(conn.prepare("SELECT * FROM categories \
49-
WHERE category = $1"));
50-
let rows = try!(stmt.query(&[&name]));
48+
let stmt = conn.prepare("SELECT * FROM categories \
49+
WHERE category = $1")?;
50+
let rows = stmt.query(&[&name])?;
5151
rows.iter().next()
5252
.chain_error(|| NotFound)
5353
.map(|row| Model::from_row(&row))
5454
}
5555

5656
pub fn find_by_slug(conn: &GenericConnection, slug: &str)
5757
-> CargoResult<Category> {
58-
let stmt = try!(conn.prepare("SELECT * FROM categories \
59-
WHERE slug = LOWER($1)"));
60-
let rows = try!(stmt.query(&[&slug]));
58+
let stmt = conn.prepare("SELECT * FROM categories \
59+
WHERE slug = LOWER($1)")?;
60+
let rows = stmt.query(&[&slug])?;
6161
rows.iter().next()
6262
.chain_error(|| NotFound)
6363
.map(|row| Model::from_row(&row))
@@ -80,7 +80,7 @@ impl Category {
8080
pub fn update_crate(conn: &GenericConnection,
8181
krate: &Crate,
8282
categories: &[String]) -> CargoResult<Vec<String>> {
83-
let old_categories = try!(krate.categories(conn));
83+
let old_categories = krate.categories(conn)?;
8484
let old_categories_ids: HashSet<_> = old_categories.iter().map(|cat| {
8585
cat.id
8686
}).collect();
@@ -112,21 +112,21 @@ impl Category {
112112
.collect();
113113

114114
if !to_rm.is_empty() {
115-
try!(conn.execute("DELETE FROM crates_categories \
115+
conn.execute("DELETE FROM crates_categories \
116116
WHERE category_id = ANY($1) \
117117
AND crate_id = $2",
118-
&[&to_rm, &krate.id]));
118+
&[&to_rm, &krate.id])?;
119119
}
120120

121121
if !to_add.is_empty() {
122122
let insert: Vec<_> = to_add.into_iter().map(|id| {
123123
format!("({}, {})", krate.id, id)
124124
}).collect();
125125
let insert = insert.join(", ");
126-
try!(conn.execute(&format!("INSERT INTO crates_categories \
126+
conn.execute(&format!("INSERT INTO crates_categories \
127127
(crate_id, category_id) VALUES {}",
128-
insert),
129-
&[]));
128+
insert),
129+
&[])?;
130130
}
131131

132132
Ok(invalid_categories)
@@ -139,8 +139,8 @@ impl Category {
139139
WHERE category NOT LIKE '%::%'",
140140
Model::table_name(None::<Self>
141141
));
142-
let stmt = try!(conn.prepare(&sql));
143-
let rows = try!(stmt.query(&[]));
142+
let stmt = conn.prepare(&sql)?;
143+
let rows = stmt.query(&[])?;
144144
Ok(rows.iter().next().unwrap().get("count"))
145145
}
146146

@@ -156,7 +156,7 @@ impl Category {
156156

157157
// Collect all the top-level categories and sum up the crates_cnt of
158158
// the crates in all subcategories
159-
let stmt = try!(conn.prepare(&format!(
159+
let stmt = conn.prepare(&format!(
160160
"SELECT c.id, c.category, c.slug, c.description, c.created_at, \
161161
COALESCE (( \
162162
SELECT sum(c2.crates_cnt)::int \
@@ -167,10 +167,10 @@ impl Category {
167167
FROM categories as c \
168168
WHERE c.category NOT LIKE '%::%' {} \
169169
LIMIT $1 OFFSET $2",
170-
sort_sql
171-
)));
170+
sort_sql
171+
))?;
172172

173-
let categories: Vec<_> = try!(stmt.query(&[&limit, &offset]))
173+
let categories: Vec<_> = stmt.query(&[&limit, &offset])?
174174
.iter()
175175
.map(|row| Model::from_row(&row))
176176
.collect();
@@ -180,7 +180,7 @@ impl Category {
180180

181181
pub fn subcategories(&self, conn: &GenericConnection)
182182
-> CargoResult<Vec<Category>> {
183-
let stmt = try!(conn.prepare("\
183+
let stmt = conn.prepare("\
184184
SELECT c.id, c.category, c.slug, c.description, c.created_at, \
185185
COALESCE (( \
186186
SELECT sum(c2.crates_cnt)::int \
@@ -190,9 +190,9 @@ impl Category {
190190
), 0) as crates_cnt \
191191
FROM categories as c \
192192
WHERE c.category ILIKE $1 || '::%' \
193-
AND c.category NOT ILIKE $1 || '::%::%'"));
193+
AND c.category NOT ILIKE $1 || '::%::%'")?;
194194

195-
let rows = try!(stmt.query(&[&self.category]));
195+
let rows = stmt.query(&[&self.category])?;
196196
Ok(rows.iter().map(|r| Model::from_row(&r)).collect())
197197
}
198198
}
@@ -213,16 +213,16 @@ impl Model for Category {
213213

214214
/// Handles the `GET /categories` route.
215215
pub fn index(req: &mut Request) -> CargoResult<Response> {
216-
let conn = try!(req.tx());
217-
let (offset, limit) = try!(req.pagination(10, 100));
216+
let conn = req.tx()?;
217+
let (offset, limit) = req.pagination(10, 100)?;
218218
let query = req.query();
219219
let sort = query.get("sort").map_or("alpha", String::as_str);
220220

221-
let categories = try!(Category::toplevel(conn, sort, limit, offset));
221+
let categories = Category::toplevel(conn, sort, limit, offset)?;
222222
let categories = categories.into_iter().map(Category::encodable).collect();
223223

224224
// Query for the total count of categories
225-
let total = try!(Category::count_toplevel(conn));
225+
let total = Category::count_toplevel(conn)?;
226226

227227
#[derive(RustcEncodable)]
228228
struct R { categories: Vec<EncodableCategory>, meta: Meta }
@@ -238,9 +238,9 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
238238
/// Handles the `GET /categories/:category_id` route.
239239
pub fn show(req: &mut Request) -> CargoResult<Response> {
240240
let slug = &req.params()["category_id"];
241-
let conn = try!(req.tx());
242-
let cat = try!(Category::find_by_slug(&*conn, &slug));
243-
let subcats = try!(cat.subcategories(&*conn)).into_iter().map(|s| {
241+
let conn = req.tx()?;
242+
let cat = Category::find_by_slug(&*conn, &slug)?;
243+
let subcats = cat.subcategories(&*conn)?.into_iter().map(|s| {
244244
s.encodable()
245245
}).collect();
246246
let cat = cat.encodable();
@@ -261,10 +261,10 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {
261261

262262
/// Handles the `GET /category_slugs` route.
263263
pub fn slugs(req: &mut Request) -> CargoResult<Response> {
264-
let conn = try!(req.tx());
265-
let stmt = try!(conn.prepare("SELECT slug FROM categories \
266-
ORDER BY slug"));
267-
let rows = try!(stmt.query(&[]));
264+
let conn = req.tx()?;
265+
let stmt = conn.prepare("SELECT slug FROM categories \
266+
ORDER BY slug")?;
267+
let rows = stmt.query(&[])?;
268268

269269
#[derive(RustcEncodable)]
270270
struct Slug { id: String, slug: String }

src/db.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn tls_handshake() -> Box<TlsHandshake + Sync + Send> {
5858
_domain: &str,
5959
stream: Stream)
6060
-> Result<Box<TlsStream>, Box<Error + Send + Sync>> {
61-
let stream = try!(self.0.danger_connect_without_providing_domain_for_certificate_verification_and_server_name_indication(stream));
61+
let stream = self.0.danger_connect_without_providing_domain_for_certificate_verification_and_server_name_indication(stream)?;
6262
Ok(Box::new(stream))
6363
}
6464
}
@@ -131,9 +131,9 @@ impl Transaction {
131131

132132
pub fn conn(&self) -> CargoResult<&r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManager>> {
133133
if !self.slot.filled() {
134-
let conn = try!(self.app.database.get().map_err(|e| {
134+
let conn = self.app.database.get().map_err(|e| {
135135
internal(format!("failed to get a database connection: {}", e))
136-
}));
136+
})?;
137137
self.slot.fill(Box::new(conn));
138138
}
139139
Ok(&**self.slot.borrow().unwrap())
@@ -146,8 +146,8 @@ impl Transaction {
146146
// desired effect.
147147
unsafe {
148148
if !self.tx.filled() {
149-
let conn = try!(self.conn());
150-
let t = try!(conn.transaction());
149+
let conn = self.conn()?;
150+
let t = conn.transaction()?;
151151
let t = mem::transmute::<_, pg::transaction::Transaction<'static>>(t);
152152
self.tx.fill(t);
153153
}
@@ -183,9 +183,9 @@ impl Middleware for TransactionMiddleware {
183183
if res.is_ok() && tx.commit.get() == Some(true) {
184184
transaction.set_commit();
185185
}
186-
try!(transaction.finish().map_err(|e| {
187-
Box::new(e) as Box<Error+Send>
188-
}));
186+
transaction.finish().map_err(|e| {
187+
Box::new(e) as Box<Error + Send>
188+
})?;
189189
}
190190
return res
191191
}

src/dependency.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ impl Dependency {
5151
-> CargoResult<Dependency> {
5252
let req = req.to_string();
5353
let features = features.join(",");
54-
let stmt = try!(conn.prepare("INSERT INTO dependencies
54+
let stmt = conn.prepare("INSERT INTO dependencies
5555
(version_id, crate_id, req, optional,
5656
default_features, features, target, kind)
5757
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
58-
RETURNING *"));
59-
let rows = try!(stmt.query(&[&version_id, &crate_id, &req,
60-
&optional, &default_features,
61-
&features, target, &(kind as i32)]));
58+
RETURNING *")?;
59+
let rows = stmt.query(&[&version_id, &crate_id, &req,
60+
&optional, &default_features,
61+
&features, target, &(kind as i32)])?;
6262
Ok(Model::from_row(&rows.iter().next().unwrap()))
6363
}
6464

0 commit comments

Comments
 (0)