use reqwest::StatusCode;
use serde::de::DeserializeOwned;
use serde_json::Value;
use crate::{Error, Result};
#[derive(Debug)]
pub(crate) struct Reply<T> {
pub body: T,
pub msg_id: Option<String>,
}
#[derive(Debug)]
pub(crate) struct Rejection {
pub error: Error,
pub not_executed: bool,
}
impl From<Error> for Rejection {
fn from(error: Error) -> Self {
Self {
error,
not_executed: false,
}
}
}
impl From<reqwest::Error> for Rejection {
fn from(e: reqwest::Error) -> Self {
Error::http(e).into()
}
}
pub(crate) fn parse<T: DeserializeOwned>(status: StatusCode, bytes: &[u8]) -> Result<Reply<T>> {
parse_classified(status, bytes).map_err(|r| r.error)
}
pub(crate) fn parse_classified<T: DeserializeOwned>(
status: StatusCode,
bytes: &[u8],
) -> Result<Reply<T>, Rejection> {
let auth_status = matches!(status, StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN);
let fail = |error: Error, not_executed: bool| Rejection {
error,
not_executed,
};
let Ok(value) = serde_json::from_slice::<Value>(bytes) else {
let snippet = snippet(bytes);
return Err(match classify_status(status, &snippet) {
Some(error) => fail(error, auth_status),
None => fail(
Error::UnexpectedResponse(format!("HTTP {status}: {snippet}")),
false,
),
});
};
if let Some(ret_code) = value.get("retCode").and_then(Value::as_str) {
if ret_code != "S" {
let code = match value.get("resCode") {
Some(Value::String(s)) => s.clone(),
Some(Value::Number(n)) => n.to_string(),
_ => "?".to_owned(),
};
let code = code.as_str();
let message = match value.get("resMsg") {
Some(Value::String(s)) => s.clone(),
Some(other) => other.to_string(),
None => String::new(),
};
let proven = auth_status || matches!(code, "7501" | "4002");
return Err(fail(Error::from_res_code(code, message), proven));
}
if !status.is_success() {
let error = classify_status(status, &value.to_string())
.unwrap_or_else(|| http_api_error(status, value.to_string()));
return Err(fail(error, auth_status));
}
let msg_id = match value.get("msgId") {
Some(Value::String(s)) => Some(s.clone()),
Some(Value::Number(n)) => Some(n.to_string()),
_ => None,
};
let payload = value.get("resMsg").cloned().unwrap_or(Value::Null);
return Ok(Reply {
body: decode(payload)?,
msg_id,
});
}
let oauth_error = value.get("error").filter(|v| !v.is_null()).map(|error| {
value
.get("error_description")
.filter(|v| !v.is_null())
.unwrap_or(error)
.as_str()
.map_or_else(|| error.to_string(), str::to_owned)
});
if !status.is_success() {
let message = oauth_error.unwrap_or_else(|| value.to_string());
let error =
classify_status(status, &message).unwrap_or_else(|| http_api_error(status, message));
return Err(fail(error, auth_status));
}
if let Some(message) = oauth_error {
let error = if looks_like_auth_failure(&message) {
Error::Authentication(message)
} else {
http_api_error(status, message)
};
return Err(fail(error, false));
}
Ok(Reply {
body: decode(value)?,
msg_id: None,
})
}
fn classify_status(status: StatusCode, message: &str) -> Option<Error> {
match status.as_u16() {
401 | 403 => Some(Error::Authentication(if message.is_empty() {
format!("HTTP {status}")
} else {
message.to_owned()
})),
429 => Some(Error::RateLimited),
502..=504 => Some(Error::ServiceUnavailable),
_ => None,
}
}
fn http_api_error(status: StatusCode, message: String) -> Error {
Error::Api {
code: status.as_u16().to_string(),
message,
}
}
fn looks_like_auth_failure(message: &str) -> bool {
let m = message.to_ascii_lowercase();
[
"auth",
"token",
"expired",
"invalid_grant",
"invalid_client",
"credential",
"password",
"login",
]
.iter()
.any(|needle| m.contains(needle))
}
fn snippet(bytes: &[u8]) -> String {
String::from_utf8_lossy(&bytes[..bytes.len().min(200)]).into_owned()
}
fn decode<T: DeserializeOwned>(value: Value) -> Result<T> {
serde_json::from_value(value).map_err(|e| Error::UnexpectedResponse(e.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
fn run<T: DeserializeOwned>(json: &str) -> Result<Reply<T>> {
parse(StatusCode::OK, json.as_bytes())
}
#[test]
fn success_unwraps_payload_and_msg_id() {
let r: Reply<Value> =
run(r#"{"retCode":"S","resCode":"0000","resMsg":{"a":1},"msgId":"m1"}"#).unwrap();
assert_eq!(r.body["a"], 1);
assert_eq!(r.msg_id.as_deref(), Some("m1"));
}
#[test]
fn failure_codes_map_to_errors() {
let cases = [
("5091", "RateLimited"),
("4002", "InvalidDeviceId"),
("4004", "DuplicateRequest"),
("9999", "CarTimeout"),
];
for (code, expected) in cases {
let json = format!(r#"{{"retCode":"F","resCode":"{code}","resMsg":"x"}}"#);
let err = run::<Value>(&json).unwrap_err();
assert!(
format!("{err:?}").starts_with(expected),
"{code} -> {err:?}"
);
}
}
#[test]
fn unknown_failure_keeps_code_and_message() {
let err = run::<Value>(r#"{"retCode":"F","resCode":"1234","resMsg":"Nope"}"#).unwrap_err();
assert!(
matches!(err, Error::Api { ref code, ref message } if code == "1234" && message == "Nope")
);
}
#[test]
fn oauth_error_is_authentication() {
let err = run::<Value>(r#"{"error":"Key not authorized: Token is expired"}"#).unwrap_err();
assert!(matches!(err, Error::Authentication(m) if m.contains("expired")));
}
#[test]
fn non_json_is_reported_with_status() {
let err = parse::<Value>(StatusCode::INTERNAL_SERVER_ERROR, b"<html>").unwrap_err();
assert!(matches!(err, Error::UnexpectedResponse(m) if m.contains("500")));
let err = parse::<Value>(StatusCode::BAD_GATEWAY, b"<html>").unwrap_err();
assert!(matches!(err, Error::ServiceUnavailable), "{err:?}");
}
fn at<T: DeserializeOwned>(status: u16, json: &str) -> Result<Reply<T>, Rejection> {
parse_classified(StatusCode::from_u16(status).unwrap(), json.as_bytes())
}
#[test]
fn gateway_errors_are_classified_by_status_not_as_auth() {
let gateway = r#"{"error":"Upstream timed out"}"#;
for (status, check) in [
(
504,
(|e| matches!(e, Error::ServiceUnavailable)) as fn(&Error) -> bool,
),
(502, |e| matches!(e, Error::ServiceUnavailable)),
(503, |e| matches!(e, Error::ServiceUnavailable)),
(429, |e| matches!(e, Error::RateLimited)),
(
500,
|e| matches!(e, Error::Api { code, .. } if code == "500"),
),
(
400,
|e| matches!(e, Error::Api { code, .. } if code == "400"),
),
] {
let r = at::<Value>(status, gateway).unwrap_err();
assert!(check(&r.error), "{status} -> {:?}", r.error);
assert!(!r.not_executed, "{status} must not allow a resend");
}
}
#[test]
fn http_401_and_403_are_recoverable_auth_failures() {
for body in ["", "<html>", "{}", r#"{"error":"Key not authorized"}"#] {
for status in [401, 403] {
let r = at::<Value>(status, body).unwrap_err();
assert!(matches!(r.error, Error::Authentication(_)), "{r:?}");
assert!(r.not_executed);
}
}
}
#[test]
fn only_retcode_s_is_success() {
let r = at::<Value>(200, r#"{"retCode":"X","resCode":"1234","resMsg":"?"}"#).unwrap_err();
assert!(matches!(r.error, Error::Api { ref code, .. } if code == "1234"));
let r = at::<Value>(500, r#"{"retCode":"S","resCode":"0000","resMsg":{}}"#).unwrap_err();
assert!(matches!(r.error, Error::Api { ref code, .. } if code == "500"));
}
#[test]
fn session_and_device_rejections_prove_non_execution() {
for (code, proven) in [
("7501", true),
("4002", true),
("9999", false),
("5031", false),
] {
let json = format!(r#"{{"retCode":"F","resCode":"{code}","resMsg":"x"}}"#);
assert_eq!(
at::<Value>(200, &json).unwrap_err().not_executed,
proven,
"{code}"
);
}
}
#[test]
fn numeric_res_codes_are_mapped_like_strings() {
for (code, proven) in [(7501, true), (4002, true), (5091, false)] {
let json = format!(r#"{{"retCode":"F","resCode":{code},"resMsg":"x"}}"#);
let r = at::<Value>(200, &json).unwrap_err();
assert_eq!(r.not_executed, proven, "{code}");
let expected = match code {
7501 => matches!(r.error, Error::Authentication(_)),
4002 => matches!(r.error, Error::InvalidDeviceId),
_ => matches!(r.error, Error::RateLimited),
};
assert!(expected, "{code} -> {:?}", r.error);
}
let r = at::<Value>(200, r#"{"retCode":"F","resCode":[1],"resMsg":"x"}"#).unwrap_err();
assert!(matches!(r.error, Error::Api { ref code, .. } if code == "?"));
}
#[test]
fn null_error_is_not_an_error() {
let r: Reply<Value> = run(r#"{"error":null,"access_token":"a"}"#).unwrap();
assert_eq!(r.body["access_token"], "a");
}
#[test]
fn non_auth_error_string_on_2xx_is_api() {
let err = run::<Value>(r#"{"error":"something odd"}"#).unwrap_err();
assert!(
matches!(err, Error::Api { ref code, .. } if code == "200"),
"{err:?}"
);
}
}