use std::fmt;
use http::HeaderMap;
use serde::{Deserialize, Deserializer};
use serde_json::Value;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ErrorItem {
pub reason: String,
pub message: String,
}
#[derive(Debug, Clone, Default)]
pub struct ApiError {
pub code: u16,
pub message: String,
pub details: Vec<Value>,
pub body: String,
pub header: HeaderMap,
pub errors: Vec<ErrorItem>,
}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.errors.is_empty() && self.message.is_empty() {
return write!(
f,
"googleapi: got HTTP response code {} with body: {}",
self.code, self.body
);
}
let mut buf = format!("googleapi: Error {}: ", self.code);
buf.push_str(&self.message);
if !self.details.is_empty()
&& let Ok(compact) = crate::to_go_json(&self.details)
{
buf.push_str("\nDetails:");
buf.push('\n');
buf.push_str(&go_json_indent(&compact));
buf.push('\n');
}
if self.errors.is_empty() {
return f.write_str(buf.trim());
}
if self.errors.len() == 1 && self.errors[0].message == self.message {
buf.push_str(", ");
buf.push_str(&self.errors[0].reason);
return f.write_str(&buf);
}
buf.push_str("\nMore details:\n");
for e in &self.errors {
buf.push_str(&format!("Reason: {}, Message: {}\n", e.reason, e.message));
}
f.write_str(&buf)
}
}
impl std::error::Error for ApiError {}
fn go_json_indent(compact: &[u8]) -> String {
let text = String::from_utf8_lossy(compact);
let mut out = String::with_capacity(text.len() * 2);
let mut depth = 0usize;
let mut in_string = false;
let mut escaped = false;
let mut need_indent = false;
let newline = |out: &mut String, depth: usize| {
out.push('\n');
for _ in 0..depth {
out.push_str(" ");
}
};
for c in text.chars() {
if in_string {
out.push(c);
if escaped {
escaped = false;
} else if c == '\\' {
escaped = true;
} else if c == '"' {
in_string = false;
}
continue;
}
match c {
'{' | '[' => {
if need_indent {
newline(&mut out, depth);
}
out.push(c);
depth += 1;
need_indent = true;
}
'}' | ']' => {
depth = depth.saturating_sub(1);
if need_indent {
need_indent = false;
} else {
newline(&mut out, depth);
}
out.push(c);
}
',' => {
out.push(c);
newline(&mut out, depth);
}
':' => out.push_str(": "),
_ => {
if need_indent {
newline(&mut out, depth);
need_indent = false;
}
if c == '"' {
in_string = true;
}
out.push(c);
}
}
}
out
}
pub fn check_response(parts: &http::response::Parts, body: &[u8]) -> Result<(), Box<ApiError>> {
if parts.status.is_success() {
return Ok(());
}
Err(Box::new(api_error_from_response(parts, body)))
}
pub fn api_error_from_response(parts: &http::response::Parts, body: &[u8]) -> ApiError {
let status = parts.status.as_u16();
let mut err = ApiError {
code: status,
body: String::from_utf8_lossy(body).into_owned(),
header: parts.headers.clone(),
..ApiError::default()
};
if let Some(reply) = error_reply_from_body(body) {
if reply.code != 0 {
err.code = u16::try_from(reply.code).unwrap_or(status);
}
err.message = reply.message;
err.details = reply.details;
err.errors = reply
.errors
.into_iter()
.map(|i| {
let i = i.unwrap_or_default();
ErrorItem {
reason: i.reason,
message: i.message,
}
})
.collect();
}
err
}
#[derive(Deserialize)]
struct ErrorReply {
error: Option<ErrorReplyBody>,
}
#[derive(Deserialize)]
struct ErrorReplyBody {
#[serde(default, deserialize_with = "nullable")]
code: i64,
#[serde(default, deserialize_with = "nullable")]
message: String,
#[serde(default, deserialize_with = "nullable")]
details: Vec<Value>,
#[serde(default, deserialize_with = "nullable")]
errors: Vec<Option<ErrorItemReply>>,
}
#[derive(Default, Deserialize)]
#[serde(default)]
struct ErrorItemReply {
#[serde(deserialize_with = "nullable")]
reason: String,
#[serde(deserialize_with = "nullable")]
message: String,
}
fn error_reply_from_body(body: &[u8]) -> Option<ErrorReplyBody> {
if body.first() == Some(&b'[') {
let mut replies: Vec<ErrorReply> = serde_json::from_slice(body).ok()?;
if replies.is_empty() {
return None;
}
return replies.swap_remove(0).error;
}
serde_json::from_slice::<ErrorReply>(body).ok()?.error
}
fn nullable<'de, D, T>(d: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de> + Default,
{
Ok(Option::<T>::deserialize(d)?.unwrap_or_default())
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use serde_json::json;
use super::*;
fn parts(status: u16) -> http::response::Parts {
let (mut parts, ()) = http::Response::new(()).into_parts();
parts.status = http::StatusCode::from_u16(status).unwrap();
parts
}
#[test]
fn display_is_gos_error_text() {
let no_message = ApiError {
code: 502,
body: "bad gateway".into(),
..ApiError::default()
};
assert_eq!(
no_message.to_string(),
"googleapi: got HTTP response code 502 with body: bad gateway"
);
let one_item = ApiError {
code: 404,
message: "nf".into(),
errors: vec![ErrorItem {
reason: "notFound".into(),
message: "nf".into(),
}],
..ApiError::default()
};
assert_eq!(one_item.to_string(), "googleapi: Error 404: nf, notFound");
let many = ApiError {
code: 400,
message: "bad".into(),
errors: vec![
ErrorItem {
reason: "invalid".into(),
message: "a".into(),
},
ErrorItem::default(),
],
..ApiError::default()
};
assert_eq!(
many.to_string(),
"googleapi: Error 400: bad\nMore details:\nReason: invalid, Message: a\nReason: , \
Message: \n"
);
let details = ApiError {
code: 400,
message: "bad".into(),
details: vec![json!({"@type": "t<x>", "empty": {}, "list": [1, 2], "none": []})],
..ApiError::default()
};
assert_eq!(
details.to_string(),
"googleapi: Error 400: bad\nDetails:\n[\n {\n \"@type\": \"t\\u003cx\\u003e\",\n \
\"empty\": {},\n \"list\": [\n 1,\n 2\n ],\n \"none\": []\n }\n]"
);
}
#[test]
fn check_response_passes_2xx_and_builds_the_error_otherwise() {
for status in [200, 201, 204, 299] {
assert!(
check_response(&parts(status), b"not json").is_ok(),
"{status}"
);
}
let mut p = parts(404);
p.headers
.insert("x-goog-request-id", http::HeaderValue::from_static("r1"));
let body = br#"{"error":{"code":404,"message":"gone","errors":[{"reason":"notFound","message":"gone"}],"details":[{"k":1}]}}"#;
let err = check_response(&p, body).unwrap_err();
assert_eq!(err.code, 404);
assert_eq!(err.message, "gone");
assert_eq!(err.details, vec![json!({"k": 1})]);
assert_eq!(err.body, String::from_utf8_lossy(body));
assert_eq!(err.header.get("x-goog-request-id").unwrap(), "r1");
let err = check_response(&parts(304), b"").unwrap_err();
assert_eq!(
err.to_string(),
"googleapi: got HTTP response code 304 with body: "
);
}
#[test]
fn api_error_from_response_reads_go_error_replies() {
let err =
api_error_from_response(&parts(400), br#"[{"error":{"code":409,"message":"dup"}}]"#);
assert_eq!((err.code, err.message.as_str()), (409, "dup"));
for body in [
&br#"{"error":{"code":0,"message":"m"}}"#[..],
br#"{"error":{"code":-1,"message":"m"}}"#,
br#"{"error":{"code":70000,"message":"m"}}"#,
] {
assert_eq!(api_error_from_response(&parts(412), body).code, 412);
}
let err = api_error_from_response(
&parts(403),
br#"{"error":{"code":null,"message":null,"details":null,"errors":[null,{"reason":null,"message":"m"}]}}"#,
);
assert_eq!(err.code, 403);
assert_eq!(
err.errors,
vec![
ErrorItem::default(),
ErrorItem {
reason: String::new(),
message: "m".into(),
},
]
);
for body in [
&b"down"[..],
b"[]",
br#"{"error":null}"#,
br#"{"error":"x"}"#,
] {
let err = api_error_from_response(&parts(502), body);
assert_eq!(err.code, 502);
assert!(err.message.is_empty() && err.errors.is_empty());
assert_eq!(err.body, String::from_utf8_lossy(body));
}
}
}