|
| 1 | +//! Errors handling |
| 2 | +
|
| 3 | +use std::fmt; |
| 4 | + |
| 5 | +use crate::interactions::REPORT_TO; |
| 6 | + |
| 7 | +use axum::{ |
| 8 | + http::StatusCode, |
| 9 | + response::{IntoResponse, Response}, |
| 10 | +}; |
| 11 | + |
| 12 | +/// Represent a user error. |
| 13 | +/// |
| 14 | +/// The message will be shown to the user via comment posted by this bot. |
| 15 | +#[derive(Debug)] |
| 16 | +pub enum UserError { |
| 17 | + /// Simple message |
| 18 | + Message(String), |
| 19 | + /// Unknown labels |
| 20 | + UnknownLabels { labels: Vec<String> }, |
| 21 | + /// Invalid assignee |
| 22 | + InvalidAssignee, |
| 23 | +} |
| 24 | + |
| 25 | +impl std::error::Error for UserError {} |
| 26 | + |
| 27 | +// NOTE: This is used to post the Github comment; make sure it's valid markdown. |
| 28 | +impl fmt::Display for UserError { |
| 29 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 30 | + match self { |
| 31 | + UserError::Message(msg) => f.write_str(msg), |
| 32 | + UserError::UnknownLabels { labels } => { |
| 33 | + write!(f, "Unknown labels: {}", labels.join(", ")) |
| 34 | + } |
| 35 | + UserError::InvalidAssignee => write!(f, "invalid assignee"), |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Creates a [`UserError`] with message. |
| 41 | +/// |
| 42 | +/// Should be used when an handler is in error due to the user action's (not a PR, |
| 43 | +/// not a issue, not authorized, ...). |
| 44 | +/// |
| 45 | +/// Should be used like this `return user_error!("My error message.");`. |
| 46 | +macro_rules! user_error { |
| 47 | + ($err:expr $(,)?) => { |
| 48 | + anyhow::Result::Err(anyhow::anyhow!(crate::errors::UserError::Message( |
| 49 | + $err.into() |
| 50 | + ))) |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +// export the macro |
| 55 | +pub(crate) use user_error; |
| 56 | + |
| 57 | +/// Represent a application error. |
| 58 | +/// |
| 59 | +/// Useful for returning a error via the API |
| 60 | +pub struct AppError(anyhow::Error); |
| 61 | + |
| 62 | +impl IntoResponse for AppError { |
| 63 | + fn into_response(self) -> Response { |
| 64 | + tracing::error!("{:?}", &self.0); |
| 65 | + ( |
| 66 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 67 | + format!("Something went wrong: {}\n\n{REPORT_TO}", self.0), |
| 68 | + ) |
| 69 | + .into_response() |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl<E> From<E> for AppError |
| 74 | +where |
| 75 | + E: Into<anyhow::Error>, |
| 76 | +{ |
| 77 | + fn from(err: E) -> Self { |
| 78 | + AppError(err.into()) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Represent an error when trying to assign someone |
| 83 | +#[derive(Debug)] |
| 84 | +pub enum AssignmentError { |
| 85 | + InvalidAssignee, |
| 86 | + Other(anyhow::Error), |
| 87 | +} |
| 88 | + |
| 89 | +// NOTE: This is used to post the Github comment; make sure it's valid markdown. |
| 90 | +impl fmt::Display for AssignmentError { |
| 91 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 92 | + match self { |
| 93 | + AssignmentError::InvalidAssignee => write!(f, "invalid assignee"), |
| 94 | + AssignmentError::Other(err) => write!(f, "{err}"), |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl From<AssignmentError> for anyhow::Error { |
| 100 | + fn from(a: AssignmentError) -> Self { |
| 101 | + match a { |
| 102 | + AssignmentError::InvalidAssignee => UserError::InvalidAssignee.into(), |
| 103 | + AssignmentError::Other(err) => err.context("assignment error"), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod tests { |
| 110 | + use super::*; |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn display_labels() { |
| 114 | + let x = UserError::UnknownLabels { |
| 115 | + labels: vec!["A-bootstrap".into(), "xxx".into()], |
| 116 | + }; |
| 117 | + assert_eq!(x.to_string(), "Unknown labels: A-bootstrap, xxx"); |
| 118 | + } |
| 119 | +} |
0 commit comments