account_api 抽出 error_field 消除 message/code 的重复解析
error_message / error_code 只差字段名,合并到 error_field(body, field) 调用点保留 error_message / error_code 两个薄封装
This commit is contained in:
@@ -67,26 +67,23 @@ fn bounded_business_id(value: &str, label: &str) -> Result<String, String> {
|
||||
Ok(value.to_string())
|
||||
}
|
||||
|
||||
fn error_message(body: &str) -> Option<String> {
|
||||
fn error_field(body: &str, field: &str) -> Option<String> {
|
||||
let value = serde_json::from_str::<Value>(body).ok()?;
|
||||
let error = value.get("error").unwrap_or(&value);
|
||||
error
|
||||
.get("message")
|
||||
.get(field)
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|message| !message.is_empty())
|
||||
.filter(|detail| !detail.is_empty())
|
||||
.map(ToString::to_string)
|
||||
}
|
||||
|
||||
fn error_message(body: &str) -> Option<String> {
|
||||
error_field(body, "message")
|
||||
}
|
||||
|
||||
fn error_code(body: &str) -> Option<String> {
|
||||
let value = serde_json::from_str::<Value>(body).ok()?;
|
||||
let error = value.get("error").unwrap_or(&value);
|
||||
error
|
||||
.get("code")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|code| !code.is_empty())
|
||||
.map(ToString::to_string)
|
||||
error_field(body, "code")
|
||||
}
|
||||
|
||||
fn map_http_error(status: StatusCode, body: &str, fallback: &str) -> String {
|
||||
|
||||
Reference in New Issue
Block a user