Skip to content
Draft
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
158 changes: 157 additions & 1 deletion apps/server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! { "error": { "code": "not_found", "message": "文档不存在" } }
//! ```
//! 前端与 SDK 依赖 `code` 做判断,`message` 仅用于展示与日志。
//! 字段校验错误额外包含‘error.fields'数组

use axum::{
Json,
Expand All @@ -15,11 +16,21 @@ use serde::Serialize;
use thiserror::Error;

/// 应用层统一错误类型。

#[derive(Debug,Serialize)]
pub struct FieldError{
pub field: String,
pub message: String,
}

#[derive(Debug, Error)]
pub enum AppError {
/// 请求参数非法(400)。
#[error("{0}")]
BadRequest(String),
///请求字段(用户输入)校验失败(400)。
#[error("请求参数校验失败")]
Validation(Vec<FieldError>),
/// 未认证或凭证失效(401)。
#[error("{0}")]
Unauthorized(String),
Expand Down Expand Up @@ -47,6 +58,7 @@ impl AppError {
Self::NotFound(_) => "not_found",
Self::Conflict(_) => "conflict",
Self::Internal(_) => "internal_error",
Self::Validation(_) => "validation_failed",
}
}

Expand All @@ -59,6 +71,7 @@ impl AppError {
Self::NotFound(_) => StatusCode::NOT_FOUND,
Self::Conflict(_) => StatusCode::CONFLICT,
Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::Validation(_) => StatusCode::BAD_REQUEST,
}
}
}
Expand All @@ -68,6 +81,9 @@ impl AppError {
struct ErrorBody {
code: &'static str,
message: String,

#[serde(skip_serializing_if = "Option::is_none")]
fields: Option<Vec<FieldError>>,
}

#[derive(Debug, Serialize)]
Expand All @@ -87,8 +103,17 @@ impl IntoResponse for AppError {
tracing::debug!(status = status.as_u16(), code, "request rejected");
}

let fields= match self{
Self::Validation(fields) => Some(fields),
_ => None,
};

let body = Json(ErrorResponse {
error: ErrorBody { code, message },
error: ErrorBody {
code,
message,
fields,
},
});

(status, body).into_response()
Expand Down Expand Up @@ -127,4 +152,135 @@ mod tests {
assert_eq!(error.to_string(), "服务器内部错误");
assert_eq!(error.status(), StatusCode::INTERNAL_SERVER_ERROR);
}

#[test]

fn validation_errors_should_return_400() {
let error = AppError::Validation(vec![
FieldError{
field: "email".to_owned(),
message:"邮箱输错了".to_owned(),
}
]);
assert_eq!(error.to_string(),"请求参数校验失败")
}

#[tokio::test]
async fn existing_errors_keep_response_shape(){
let cases = [
(
AppError::BadRequest("参数不正确".to_owned()),
StatusCode::BAD_REQUEST,
"bad_request",
"参数不正确",
),

(
AppError::Unauthorized("请先登录".to_owned()),
StatusCode::UNAUTHORIZED,
"unauthorized",
"请先登录",
),

(
AppError::Forbidden("没有权限".to_owned()),
StatusCode::FORBIDDEN,
"forbidden",
"没有权限",
),

(
AppError::NotFound("文档不存在".to_owned()),
StatusCode::NOT_FOUND,
"not_found",
"文档不存在",
),

(
AppError::Conflict("文档已存在".to_owned()),
StatusCode::CONFLICT,
"conflict",
"文档已存在",
),

(
AppError::Internal(anyhow::anyhow!("服务器内部错误")),
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"服务器内部错误",
),
];

for(error, expected_status, expected_code, expected_message) in cases {
assert_eq!(error.status(),expected_status);
assert_eq!(error.code(),expected_code);
let response = error.into_response();
assert_eq!(response.status(),expected_status);
let bytes = axum::body::to_bytes(response.into_body(),1024 * 1024)
.await
.expect("响应体应该可以读取");

let actual: serde_json::Value =
serde_json::from_slice(&bytes).expect("响应体应该是合法的 JSON");

assert_eq!(
actual,
serde_json::json!({
"error":{
"code":expected_code,
"message":expected_message,
}
}),
"旧错误响应发生变化:{expected_code}"
);
}

}

#[tokio::test]
async fn validation_errors_include_fields(){
let error = AppError::Validation(vec![
FieldError{
field: "email".to_owned(),
message:"邮箱输错了".to_owned(),
},
FieldError{
field:"password".to_owned(),
message:"密码输错了".to_owned(),
}
]);
assert_eq!(error.status(),StatusCode::BAD_REQUEST);
assert_eq!(error.code(),"validation_failed");

let response = error.into_response();
assert_eq!(response.status(),StatusCode::BAD_REQUEST);

let bytes = axum::body::to_bytes(response.into_body(),1024 * 1024)
.await
.expect("响应体应该可以读取");

let actual: serde_json::Value =
serde_json::from_slice(&bytes).expect("响应体应该是合法的 JSON");

assert_eq!(
actual,
serde_json::json!({
"error":{
"code":"validation_failed",
"message":"请求参数校验失败",
"fields":[
{
"field":"email",
"message":"邮箱输错了"
},
{
"field":"password",
"message":"密码输错了"
}
]
}
})
);
}

}