Skip to content

Commit

Permalink
refactor: ApiError helpers (#1776)
Browse files Browse the repository at this point in the history
  • Loading branch information
chesedo authored May 21, 2024
1 parent 60e2d26 commit 97571c5
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion common/src/models/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ pub struct ApiError {
impl ApiError {
pub fn internal(message: &str) -> Self {
Self {
message: message.to_string(),
message: format!("{message}. Please create a ticket to get this fixed."),
status_code: StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
}
}

/// Creates an internal error without exposing sensitive information to the user.
pub fn internal_safe(error: impl std::error::Error) -> Self {
error!(error = error.to_string(), "");

Self {
message: "Internal server error occured. Please create a ticket to get this fixed."
.to_string(),
status_code: StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
}
}
Expand All @@ -43,6 +54,27 @@ impl ApiError {
}
}

pub fn not_found(message: impl ToString) -> Self {
Self {
message: message.to_string(),
status_code: StatusCode::NOT_FOUND.as_u16(),
}
}

pub fn unauthorized() -> Self {
Self {
message: "Unauthorized".to_string(),
status_code: StatusCode::UNAUTHORIZED.as_u16(),
}
}

pub fn forbidden() -> Self {
Self {
message: "Forbidden".to_string(),
status_code: StatusCode::FORBIDDEN.as_u16(),
}
}

pub fn status(&self) -> StatusCode {
StatusCode::from_u16(self.status_code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
}
Expand Down

0 comments on commit 97571c5

Please sign in to comment.