Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tracing = "0.1"

serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", optional = true }

http = "1"

# опциональные интеграции
Expand Down
48 changes: 42 additions & 6 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ impl ErrorResponse {
self.www_authenticate = Some(value.into());
self
}

/// Convert numeric [`status`](ErrorResponse::status) into [`StatusCode`].
///
/// Invalid codes default to `StatusCode::INTERNAL_SERVER_ERROR`.
///
/// # Examples
///
/// ```
/// use http::StatusCode;
/// use masterror::{AppCode, ErrorResponse};
///
/// let resp = ErrorResponse::new(404, AppCode::NotFound, "missing").expect("status");
/// assert_eq!(resp.status_code(), StatusCode::NOT_FOUND);
/// ```
#[must_use]
pub fn status_code(&self) -> StatusCode {
StatusCode::from_u16(self.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
}
}

/// Legacy constructor retained for migration purposes.
Expand Down Expand Up @@ -240,7 +258,7 @@ mod axum_impl {
use axum::{
Json,
http::{
HeaderValue, StatusCode,
HeaderValue,
header::{RETRY_AFTER, WWW_AUTHENTICATE}
},
response::{IntoResponse, Response}
Expand All @@ -251,8 +269,7 @@ mod axum_impl {

impl IntoResponse for ErrorResponse {
fn into_response(self) -> Response {
let status =
StatusCode::from_u16(self.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
let status = self.status_code();

// Serialize JSON body first (borrow self for payload).
let mut response = (status, Json(&self)).into_response();
Expand Down Expand Up @@ -299,7 +316,7 @@ mod actix_impl {
HttpRequest, HttpResponse, Responder,
body::BoxBody,
http::{
StatusCode,
StatusCode as ActixStatus,
header::{RETRY_AFTER, WWW_AUTHENTICATE}
}
};
Expand All @@ -310,8 +327,9 @@ mod actix_impl {
type Body = BoxBody;

fn respond_to(self, _req: &HttpRequest) -> HttpResponse {
let status =
StatusCode::from_u16(self.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
let status = self.status_code();
let status = ActixStatus::from_u16(status.as_u16())
.unwrap_or(ActixStatus::INTERNAL_SERVER_ERROR);

let mut builder = HttpResponse::build(status);
if let Some(retry) = self.retry {
Expand Down Expand Up @@ -360,6 +378,24 @@ mod tests {
assert_eq!(e.www_authenticate.as_deref(), Some(r#"Bearer realm="api""#));
}

#[test]
fn status_code_maps_invalid_to_internal_server_error() {
use http::StatusCode;

let valid = ErrorResponse::new(404, AppCode::NotFound, "missing").expect("status");
assert_eq!(valid.status_code(), StatusCode::NOT_FOUND);

let invalid = ErrorResponse {
status: 1000,
code: AppCode::Internal,
message: "oops".into(),
details: None,
retry: None,
www_authenticate: None
};
assert_eq!(invalid.status_code(), StatusCode::INTERNAL_SERVER_ERROR);
}

// --- Details: JSON vs text ----------------------------------------------

#[cfg(feature = "serde_json")]
Expand Down
Loading