-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
105 additions
and
102 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
pub(crate) mod cardano; | ||
pub(crate) mod error_message; | ||
pub(crate) mod legacy; | ||
pub(crate) mod server_error; |
52 changes: 52 additions & 0 deletions
52
catalyst-gateway/bin/src/service/common/objects/server_error.rs
This file contains 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,52 @@ | ||
//! Define `ServerError` type. | ||
|
||
use poem_openapi::{types::Example, Object}; | ||
use url::Url; | ||
use uuid::Uuid; | ||
|
||
/// While using macro-vis lib, you will get the `uncommon_codepoints` warning, so you will | ||
/// probably want to place this in your crate root | ||
use crate::settings::generate_github_issue_url; | ||
|
||
#[derive(Debug, Object)] | ||
#[oai(example, skip_serializing_if_is_none)] | ||
/// Server Error response to a Bad request. | ||
pub(crate) struct ServerError { | ||
/// Unique ID of this Server Error so that it can be located easily for debugging. | ||
id: Uuid, | ||
/// *Optional* SHORT Error message. | ||
/// Will not contain sensitive information, internal details or backtraces. | ||
// TODO(bkioshn): https://github.com/input-output-hk/catalyst-voices/issues/239 | ||
#[oai(validator(max_length = "100", pattern = "^[0-9a-zA-Z].*$"))] | ||
msg: String, | ||
/// A URL to report an issue. | ||
// TODO(bkioshn): https://github.com/input-output-hk/catalyst-voices/issues/239 | ||
#[oai(validator(max_length = "1000"))] | ||
issue: Option<Url>, | ||
} | ||
|
||
impl ServerError { | ||
/// Create a new Server Error Response Payload. | ||
pub(crate) fn new(msg: Option<String>) -> Self { | ||
let msg = msg.unwrap_or( | ||
"Internal Server Error. Please report the issue to the service owner.".to_string(), | ||
); | ||
let id = Uuid::new_v4(); | ||
let issue_title = format!("Internal Server Error - {id}"); | ||
let issue = generate_github_issue_url(&issue_title); | ||
|
||
Self { id, msg, issue } | ||
} | ||
|
||
/// Get the id of this Server Error. | ||
pub(crate) fn id(&self) -> Uuid { | ||
self.id | ||
} | ||
} | ||
|
||
impl Example for ServerError { | ||
/// Example for the Server Error Payload. | ||
fn example() -> Self { | ||
Self::new(Some("Server Error".to_string())) | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,6 +1,23 @@ | ||
//! Generic Responses are all contained in their own modules, grouped by response codes. | ||
#[macro_use] | ||
|
||
pub(crate) mod resp_2xx; | ||
pub(crate) mod resp_4xx; | ||
pub(crate) mod resp_5xx; | ||
|
||
/// Handle a 5xx response. | ||
/// Returns a Server Error or a Service Unavailable response. | ||
/// Logging error message. | ||
/// Argument must be `anyhow::Error` type. | ||
macro_rules! handle_5xx_response { | ||
($err:ident) => {{ | ||
if $err.is::<bb8::RunError<tokio_postgres::Error>>() { | ||
AllResponses::ServiceUnavailable | ||
} else { | ||
let error = crate::service::common::objects::server_error::ServerError::new(None); | ||
let id = error.id(); | ||
tracing::error!(id = format!("{id}"), "{}", $err); | ||
AllResponses::ServerError(Json(error)) | ||
} | ||
}}; | ||
} | ||
pub(crate) use handle_5xx_response; |