-
Notifications
You must be signed in to change notification settings - Fork 96
Improve our handling of user facing errors #2210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ba863a8
Create `errors` module and move `AppError` into it
Urgau 570732b
Move `UserError` to `errors` module
Urgau 81e1b0b
Move the `user_error` macro to the `errors` module
Urgau 6a9d377
Make `UserError` an enum for future errors
Urgau 88f7680
Move `UnknownLabels` error to `UserError` and simplify it's usage
Urgau a10c9c8
Also consider `UserError` in normal handler to be a message error
Urgau 87d1c44
Move `InvalidAssignee` to `UserError`
Urgau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| //! Errors handling | ||
| use std::fmt; | ||
|
|
||
| use crate::interactions::REPORT_TO; | ||
|
|
||
| use axum::{ | ||
| http::StatusCode, | ||
| response::{IntoResponse, Response}, | ||
| }; | ||
|
|
||
| /// Represent a user error. | ||
| /// | ||
| /// The message will be shown to the user via comment posted by this bot. | ||
| #[derive(Debug)] | ||
| pub enum UserError { | ||
| /// Simple message | ||
| Message(String), | ||
| /// Unknown labels | ||
| UnknownLabels { labels: Vec<String> }, | ||
| /// Invalid assignee | ||
| InvalidAssignee, | ||
| } | ||
|
|
||
| impl std::error::Error for UserError {} | ||
|
|
||
| // NOTE: This is used to post the Github comment; make sure it's valid markdown. | ||
| impl fmt::Display for UserError { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| match self { | ||
| UserError::Message(msg) => f.write_str(msg), | ||
| UserError::UnknownLabels { labels } => { | ||
| write!(f, "Unknown labels: {}", labels.join(", ")) | ||
| } | ||
| UserError::InvalidAssignee => write!(f, "invalid assignee"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Creates a [`UserError`] with message. | ||
| /// | ||
| /// Should be used when an handler is in error due to the user action's (not a PR, | ||
| /// not a issue, not authorized, ...). | ||
| /// | ||
| /// Should be used like this `return user_error!("My error message.");`. | ||
| macro_rules! user_error { | ||
| ($err:expr $(,)?) => { | ||
| anyhow::Result::Err(anyhow::anyhow!(crate::errors::UserError::Message( | ||
| $err.into() | ||
| ))) | ||
| }; | ||
| } | ||
|
|
||
| // export the macro | ||
| pub(crate) use user_error; | ||
|
|
||
| /// Represent a application error. | ||
| /// | ||
| /// Useful for returning a error via the API | ||
| pub struct AppError(anyhow::Error); | ||
|
|
||
| impl IntoResponse for AppError { | ||
| fn into_response(self) -> Response { | ||
| tracing::error!("{:?}", &self.0); | ||
| ( | ||
| StatusCode::INTERNAL_SERVER_ERROR, | ||
| format!("Something went wrong: {}\n\n{REPORT_TO}", self.0), | ||
| ) | ||
| .into_response() | ||
| } | ||
| } | ||
|
|
||
| impl<E> From<E> for AppError | ||
| where | ||
| E: Into<anyhow::Error>, | ||
| { | ||
| fn from(err: E) -> Self { | ||
| AppError(err.into()) | ||
| } | ||
| } | ||
|
|
||
| /// Represent an error when trying to assign someone | ||
| #[derive(Debug)] | ||
| pub enum AssignmentError { | ||
| InvalidAssignee, | ||
| Other(anyhow::Error), | ||
| } | ||
|
|
||
| // NOTE: This is used to post the Github comment; make sure it's valid markdown. | ||
| impl fmt::Display for AssignmentError { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| match self { | ||
| AssignmentError::InvalidAssignee => write!(f, "invalid assignee"), | ||
| AssignmentError::Other(err) => write!(f, "{err}"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<AssignmentError> for anyhow::Error { | ||
| fn from(a: AssignmentError) -> Self { | ||
| match a { | ||
| AssignmentError::InvalidAssignee => UserError::InvalidAssignee.into(), | ||
| AssignmentError::Other(err) => err.context("assignment error"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn display_labels() { | ||
| let x = UserError::UnknownLabels { | ||
| labels: vec!["A-bootstrap".into(), "xxx".into()], | ||
| }; | ||
| assert_eq!(x.to_string(), "Unknown labels: A-bootstrap, xxx"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.