aboutsummaryrefslogtreecommitdiff
path: root/src/api/error.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2021-05-03 22:45:42 +0200
committerAlex Auvolat <alex@adnab.me>2021-05-06 22:37:15 +0200
commit6ccffc316228bb056fa135cc5fceb2ed75f545f5 (patch)
tree109e9334aaee5b7865a5d29fcfaea23010da0cc4 /src/api/error.rs
parente4b9e4e24d006373a20cfcbdac9dba5e399ee182 (diff)
downloadgarage-6ccffc316228bb056fa135cc5fceb2ed75f545f5.tar.gz
garage-6ccffc316228bb056fa135cc5fceb2ed75f545f5.zip
Improved XML serializationbetter_xml
- Use quick_xml and serde for all XML response returned by the S3 API. - Include tests for all structs used to generate XML - Remove old manual XML escaping function which was unsafe
Diffstat (limited to 'src/api/error.rs')
-rw-r--r--src/api/error.rs29
1 files changed, 17 insertions, 12 deletions
diff --git a/src/api/error.rs b/src/api/error.rs
index a49ba211..7d97366e 100644
--- a/src/api/error.rs
+++ b/src/api/error.rs
@@ -1,11 +1,9 @@
-use std::fmt::Write;
-
use err_derive::Error;
use hyper::StatusCode;
use garage_util::error::Error as GarageError;
-use crate::encoding::*;
+use crate::s3_xml;
/// Errors of this crate
#[derive(Debug, Error)]
@@ -104,15 +102,22 @@ impl Error {
}
pub fn aws_xml(&self, garage_region: &str, path: &str) -> String {
- let mut xml = String::new();
- writeln!(&mut xml, r#"<?xml version="1.0" encoding="UTF-8"?>"#).unwrap();
- writeln!(&mut xml, "<Error>").unwrap();
- writeln!(&mut xml, "\t<Code>{}</Code>", self.aws_code()).unwrap();
- writeln!(&mut xml, "\t<Message>{}</Message>", self).unwrap();
- writeln!(&mut xml, "\t<Resource>{}</Resource>", xml_escape(path)).unwrap();
- writeln!(&mut xml, "\t<Region>{}</Region>", garage_region).unwrap();
- writeln!(&mut xml, "</Error>").unwrap();
- xml
+ let error = s3_xml::Error {
+ code: s3_xml::Value(self.aws_code().to_string()),
+ message: s3_xml::Value(format!("{}", self)),
+ resource: Some(s3_xml::Value(path.to_string())),
+ region: Some(s3_xml::Value(garage_region.to_string())),
+ };
+ s3_xml::to_xml_with_header(&error).unwrap_or_else(|_| {
+ r#"
+<?xml version="1.0" encoding="UTF-8"?>
+<Error>
+ <Code>InternalError</Code>
+ <Message>XML encoding of error failed</Message>
+</Error>
+ "#
+ .into()
+ })
}
}