Skip to content

Commit 255b91b

Browse files
committed
Convert enqueue-job bin to a new concrete error type
1 parent 9fe00de commit 255b91b

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

src/bin/enqueue-job.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
use cargo_registry::util::{cargo_err, AppError, AppResult};
2-
use cargo_registry::{db, env, tasks};
3-
use diesel::PgConnection;
1+
#![deny(clippy::all)]
42

5-
fn main() -> AppResult<()> {
3+
use cargo_registry::{db, env, tasks, util::Error};
4+
use swirl::Job;
5+
6+
fn main() -> Result<(), Error> {
67
let conn = db::connect_now()?;
78
let mut args = std::env::args().skip(1);
89
match &*args.next().unwrap_or_default() {
9-
"update_downloads" => tasks::update_downloads().enqueue(&conn),
10+
"update_downloads" => Ok(tasks::update_downloads().enqueue(&conn)?),
1011
"dump_db" => {
1112
let database_url = args.next().unwrap_or_else(|| env("DATABASE_URL"));
1213
let target_name = args
1314
.next()
1415
.unwrap_or_else(|| String::from("db-dump.tar.gz"));
15-
tasks::dump_db(database_url, target_name).enqueue(&conn)
16+
Ok(tasks::dump_db(database_url, target_name).enqueue(&conn)?)
1617
}
17-
other => Err(cargo_err(&format!("Unrecognized job type `{}`", other))),
18+
other => Err(Error::from(format!("Unrecognized job type `{}`", other))),
1819
}
1920
}
20-
21-
/// Helper to map the `PerformError` returned by `swirl::Job::enqueue()` to a
22-
/// `AppError`. Can be removed once `map_err()` isn't needed any more.
23-
trait Enqueue: swirl::Job {
24-
fn enqueue(self, conn: &PgConnection) -> AppResult<()> {
25-
<Self as swirl::Job>::enqueue(self, conn).map_err(|e| AppError::from_std_error(e))
26-
}
27-
}
28-
29-
impl<J: swirl::Job> Enqueue for J {}

src/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::io::Cursor;
55
use conduit::Response;
66
use serde::Serialize;
77

8-
pub use self::errors::ChainError;
9-
pub use self::errors::{bad_request, cargo_err, internal, AppError, AppResult};
8+
pub use self::errors::concrete::Error;
9+
pub use self::errors::{bad_request, cargo_err, internal, AppError, AppResult, ChainError};
1010
pub use self::io_util::{read_fill, read_le_u32, LimitErrorReader};
1111
pub use self::request_helpers::*;
1212
pub use self::request_proxy::RequestProxy;

src/util/errors.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use diesel::result::Error as DieselError;
88

99
use crate::util::json_response;
1010

11+
pub(super) mod concrete;
1112
mod http;
1213

1314
/// Returns an error with status 200 and the provided description as JSON
@@ -181,7 +182,7 @@ impl<E: Error + Send + 'static> From<E> for Box<dyn AppError> {
181182
}
182183
}
183184
// =============================================================================
184-
// Concrete errors
185+
// Internal error for use with `chain_error`
185186

186187
#[derive(Debug)]
187188
struct InternalAppError {
@@ -201,6 +202,8 @@ impl AppError for InternalAppError {
201202
}
202203
}
203204

205+
// TODO: The remaining can probably move under `http`
206+
204207
#[derive(Debug, Clone, Copy)]
205208
pub struct NotFound;
206209

src/util/errors/concrete.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::{error, fmt};
2+
3+
#[derive(Debug)]
4+
pub enum Error {
5+
DbConnect(diesel::result::ConnectionError),
6+
Internal(String),
7+
JobEnqueue(swirl::EnqueueError),
8+
}
9+
10+
impl error::Error for Error {}
11+
12+
impl fmt::Display for Error {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
match self {
15+
Error::DbConnect(inner) => inner.fmt(f),
16+
Error::Internal(inner) => inner.fmt(f),
17+
Error::JobEnqueue(inner) => inner.fmt(f),
18+
}
19+
}
20+
}
21+
22+
impl From<diesel::result::ConnectionError> for Error {
23+
fn from(err: diesel::result::ConnectionError) -> Self {
24+
Error::DbConnect(err)
25+
}
26+
}
27+
28+
impl From<String> for Error {
29+
fn from(err: String) -> Self {
30+
Error::Internal(err)
31+
}
32+
}
33+
34+
impl From<swirl::EnqueueError> for Error {
35+
fn from(err: swirl::EnqueueError) -> Self {
36+
Error::JobEnqueue(err)
37+
}
38+
}

0 commit comments

Comments
 (0)