pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[cfg(feature = "webhook-base")]
#[error(transparent)]
Webhook(#[from] WebhookError),
}
#[cfg(feature = "webhook-base")]
#[derive(Debug, thiserror::Error)]
pub enum WebhookError {
#[cfg(feature = "reporter-webhook")]
#[error("error building Slack webhook reporter: {0}")]
ReporterBuilder(
#[from] crate::leaderbot::reporter::slack::webhook::SlackWebhookReporterBuilderError,
),
#[cfg(feature = "reporter-webhook")]
#[error("error reporting changes to Slack: {0}")]
ReportChanges(WebhookMessageError),
#[cfg(feature = "reporter-webhook")]
#[error("error reporting first bot run to Slack: {0}")]
ReportFirstRun(WebhookMessageError),
#[error("error building Slack webhook message: {0}")]
MessageBuilder(#[from] crate::slack::webhook::WebhookMessageBuilderError),
}
#[cfg(feature = "reporter-webhook")]
#[derive(veil::Redact, thiserror::Error)]
#[error(
"error sending message to Slack about leaderboard id {leaderboard_id} for year {year} in channel #{channel}: {source}"
)]
pub struct WebhookMessageError {
pub year: i32,
pub leaderboard_id: u64,
#[redact(partial)]
pub webhook_url: String,
pub channel: String,
pub source: reqwest::Error,
}
#[cfg(feature = "reporter-webhook")]
impl From<crate::leaderbot::reporter::slack::webhook::SlackWebhookReporterBuilderError> for Error {
fn from(
value: crate::leaderbot::reporter::slack::webhook::SlackWebhookReporterBuilderError,
) -> Self {
WebhookError::from(value).into()
}
}
#[cfg(feature = "webhook-base")]
impl From<crate::slack::webhook::WebhookMessageBuilderError> for Error {
fn from(value: crate::slack::webhook::WebhookMessageBuilderError) -> Self {
WebhookError::from(value).into()
}
}
#[cfg(all(test, feature = "webhook-base"))]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use assert_matches::assert_matches;
use super::*;
#[cfg(feature = "reporter-webhook")]
mod from_slack_webhook_reporter_builder_error_for_error {
use std::env;
use serial_test::serial;
use super::*;
use crate::leaderbot::reporter::slack::webhook::{
SlackWebhookReporter, WEBHOOK_URL_ENV_VAR,
};
#[test]
#[serial(slack_webhook_reporter_env)]
fn reporter_builder() {
unsafe {
env::remove_var(WEBHOOK_URL_ENV_VAR);
}
let error = SlackWebhookReporter::builder()
.build_for_test()
.unwrap_err();
let error: Error = error.into();
assert_matches!(error, Error::Webhook(WebhookError::ReporterBuilder(_)));
}
}
mod from_webhook_message_builder_error_for_error {
use super::*;
use crate::slack::webhook::WebhookMessage;
#[test]
fn message_builder() {
let error = WebhookMessage::builder().build_for_test().unwrap_err();
let error: Error = error.into();
assert_matches!(error, Error::Webhook(WebhookError::MessageBuilder(_)));
}
}
}