Skip to content

Commit 1c40f7a

Browse files
committed
Convert pagerduty bins to the concrete error type
1 parent 255b91b commit 1c40f7a

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

src/bin/monitor.rs

Lines changed: 5 additions & 5 deletions
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::AppResult};
11+
use cargo_registry::{db, schema::*, util::Error};
1212
use diesel::prelude::*;
1313

14-
fn main() -> AppResult<()> {
14+
fn main() -> Result<(), Error> {
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) -> AppResult<()> {
22+
fn check_stalled_background_jobs(conn: &PgConnection) -> Result<(), Error> {
2323
use cargo_registry::schema::background_jobs::dsl::*;
2424
use diesel::dsl::*;
2525

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

58-
fn check_spam_attack(conn: &PgConnection) -> AppResult<()> {
58+
fn check_spam_attack(conn: &PgConnection) -> Result<(), Error> {
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) -> AppResult<()> {
116116
Ok(())
117117
}
118118

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

src/bin/on_call/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cargo_registry::util::{internal, AppResult};
1+
use cargo_registry::util::Error;
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) -> AppResult<()> {
28+
pub fn send(self) -> Result<(), Error> {
2929
let api_token = dotenv::var("PAGERDUTY_API_TOKEN")?;
3030
let service_key = dotenv::var("PAGERDUTY_INTEGRATION_KEY")?;
3131

@@ -43,14 +43,15 @@ impl Event {
4343
s if s.is_success() => Ok(()),
4444
Status::BAD_REQUEST => {
4545
let error = response.json::<InvalidEvent>()?;
46-
Err(internal(&format_args!("pagerduty error: {:?}", error)))
46+
Err(format!("pagerduty error: {:?}", error))
4747
}
48-
Status::FORBIDDEN => Err(internal("rate limited by pagerduty")),
49-
n => Err(internal(&format_args!(
48+
Status::FORBIDDEN => Err("rate limited by pagerduty".to_string()),
49+
n => Err(format!(
5050
"Got a non 200 response code from pagerduty: {} with {:?}",
5151
n, response
52-
))),
52+
)),
5353
}
54+
.map_err(Into::into)
5455
}
5556
}
5657

src/bin/test-pagerduty.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ mod on_call;
1111

1212
use std::env::args;
1313

14-
fn main() {
14+
use cargo_registry::util::Error;
15+
16+
fn main() -> Result<(), Error> {
1517
let args = args().collect::<Vec<_>>();
1618

1719
let event_type = &*args[1];
@@ -32,5 +34,5 @@ fn main() {
3234
},
3335
_ => panic!("Event type must be trigger, acknowledge, or resolve"),
3436
};
35-
event.send().unwrap()
37+
event.send()
3638
}

src/util/errors/concrete.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ use std::{error, fmt};
33
#[derive(Debug)]
44
pub enum Error {
55
DbConnect(diesel::result::ConnectionError),
6+
DbQuery(diesel::result::Error),
7+
DotEnv(dotenv::Error),
68
Internal(String),
79
JobEnqueue(swirl::EnqueueError),
10+
Reqwest(reqwest::Error),
811
}
912

1013
impl error::Error for Error {}
@@ -13,8 +16,11 @@ impl fmt::Display for Error {
1316
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1417
match self {
1518
Error::DbConnect(inner) => inner.fmt(f),
19+
Error::DbQuery(inner) => inner.fmt(f),
20+
Error::DotEnv(inner) => inner.fmt(f),
1621
Error::Internal(inner) => inner.fmt(f),
1722
Error::JobEnqueue(inner) => inner.fmt(f),
23+
Error::Reqwest(inner) => inner.fmt(f),
1824
}
1925
}
2026
}
@@ -25,6 +31,18 @@ impl From<diesel::result::ConnectionError> for Error {
2531
}
2632
}
2733

34+
impl From<diesel::result::Error> for Error {
35+
fn from(err: diesel::result::Error) -> Self {
36+
Error::DbQuery(err)
37+
}
38+
}
39+
40+
impl From<dotenv::Error> for Error {
41+
fn from(err: dotenv::Error) -> Self {
42+
Error::DotEnv(err)
43+
}
44+
}
45+
2846
impl From<String> for Error {
2947
fn from(err: String) -> Self {
3048
Error::Internal(err)
@@ -36,3 +54,9 @@ impl From<swirl::EnqueueError> for Error {
3654
Error::JobEnqueue(err)
3755
}
3856
}
57+
58+
impl From<reqwest::Error> for Error {
59+
fn from(err: reqwest::Error) -> Self {
60+
Error::Reqwest(err)
61+
}
62+
}

0 commit comments

Comments
 (0)