Skip to content

Commit

Permalink
Rename InternalServerError struct to ServerError
Browse files Browse the repository at this point in the history
As it's used as the reply message for multiple server side errors, not
just `500 - InternalServerError`.
  • Loading branch information
Alenar committed Jul 2, 2024
1 parent db14085 commit d7f53d6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
11 changes: 4 additions & 7 deletions mithril-aggregator/src/http_server/routes/reply.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Serialize;
use warp::http::StatusCode;

use mithril_common::entities::{ClientError, InternalServerError};
use mithril_common::entities::{ClientError, ServerError};
use mithril_common::StdError;
use mithril_persistence::sqlite::error::{SqliteError, SQLITE_BUSY};

Expand Down Expand Up @@ -46,17 +46,14 @@ pub fn server_error<E: Into<StdError>>(error: E) -> Box<dyn warp::Reply> {
code
};

json(
&InternalServerError::new(format!("{std_error:?}")),
status_code,
)
json(&ServerError::new(format!("{std_error:?}")), status_code)
}

pub fn internal_server_error<T: Into<InternalServerError>>(message: T) -> Box<dyn warp::Reply> {
pub fn internal_server_error<T: Into<ServerError>>(message: T) -> Box<dyn warp::Reply> {
json(&message.into(), StatusCode::INTERNAL_SERVER_ERROR)
}

pub fn service_unavailable<T: Into<InternalServerError>>(message: T) -> Box<dyn warp::Reply> {
pub fn service_unavailable<T: Into<ServerError>>(message: T) -> Box<dyn warp::Reply> {
json(&message.into(), StatusCode::SERVICE_UNAVAILABLE)
}

Expand Down
22 changes: 11 additions & 11 deletions mithril-common/src/entities/http_server_error.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
use crate::StdError;
use serde::{Deserialize, Serialize};

/// Representation of a Internal Server Error raised by an http server
/// Representation of a Server Error raised by a http server
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct InternalServerError {
pub struct ServerError {
/// error message
pub message: String,
}

impl InternalServerError {
impl ServerError {
/// InternalServerError factory
pub fn new(message: String) -> InternalServerError {
InternalServerError { message }
pub fn new(message: String) -> ServerError {
ServerError { message }
}
}

impl From<String> for InternalServerError {
impl From<String> for ServerError {
fn from(message: String) -> Self {
InternalServerError::new(message)
ServerError::new(message)
}
}

impl From<&str> for InternalServerError {
impl From<&str> for ServerError {
fn from(message: &str) -> Self {
InternalServerError::new(message.to_string())
ServerError::new(message.to_string())
}
}

impl From<StdError> for InternalServerError {
impl From<StdError> for ServerError {
fn from(error: StdError) -> Self {
InternalServerError::new(format!("{error:?}"))
ServerError::new(format!("{error:?}"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion mithril-common/src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use certificate_metadata::{CertificateMetadata, StakeDistributionParty};
pub use certificate_pending::CertificatePending;
pub use epoch::{Epoch, EpochError};
pub use epoch_settings::EpochSettings;
pub use http_server_error::{ClientError, InternalServerError};
pub use http_server_error::{ClientError, ServerError};
pub use mithril_stake_distribution::MithrilStakeDistribution;
pub use protocol_message::{ProtocolMessage, ProtocolMessagePartKey, ProtocolMessagePartValue};
pub use protocol_parameters::ProtocolParameters;
Expand Down
6 changes: 3 additions & 3 deletions mithril-common/src/test_utils/apispec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ components:
// for this route, so it's the default response spec that is used.
let response = build_json_response(
StatusCode::INTERNAL_SERVER_ERROR.into(),
entities::InternalServerError::new("an error occurred".to_string()),
entities::ServerError::new("an error occurred".to_string()),
);

APISpec::from_file(&APISpec::get_default_spec_file())
Expand All @@ -474,7 +474,7 @@ components:
fn test_should_fail_when_the_status_code_is_not_the_expected_one() {
let response = build_json_response(
StatusCode::INTERNAL_SERVER_ERROR.into(),
entities::InternalServerError::new("an error occurred".to_string()),
entities::ServerError::new("an error occurred".to_string()),
);

let mut api_spec = APISpec::from_file(&APISpec::get_default_spec_file());
Expand All @@ -500,7 +500,7 @@ components:
fn test_should_be_ok_when_the_status_code_is_the_right_one() {
let response = build_json_response(
StatusCode::INTERNAL_SERVER_ERROR.into(),
entities::InternalServerError::new("an error occurred".to_string()),
entities::ServerError::new("an error occurred".to_string()),
);

APISpec::from_file(&APISpec::get_default_spec_file())
Expand Down

0 comments on commit d7f53d6

Please sign in to comment.