aboutsummaryrefslogtreecommitdiff
path: root/src/api/error.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2021-04-28 01:05:40 +0200
committerAlex Auvolat <alex@adnab.me>2021-04-28 01:05:40 +0200
commitdcfc32cf85bc6276fdff2492898c1cbb527e9b9d (patch)
tree01ec3f7476733dab8c74ecb1c72b5061f6e5c8e1 /src/api/error.rs
parent368eb354846790e9fc616d9a26ddc414748d847f (diff)
downloadgarage-dcfc32cf85bc6276fdff2492898c1cbb527e9b9d.tar.gz
garage-dcfc32cf85bc6276fdff2492898c1cbb527e9b9d.zip
Many S3 compatibility improvements:v0.2.1.5
- return XML errors - implement AuthorizationHeaderMalformed error to redirect clients to correct location (used by minio client) - implement GetBucketLocation - fix DeleteObjects XML parsing and response
Diffstat (limited to 'src/api/error.rs')
-rw-r--r--src/api/error.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/api/error.rs b/src/api/error.rs
index ad0174ad..a3cdfdbd 100644
--- a/src/api/error.rs
+++ b/src/api/error.rs
@@ -1,8 +1,12 @@
+use std::fmt::Write;
+
use err_derive::Error;
use hyper::StatusCode;
use garage_util::error::Error as GarageError;
+use crate::encoding::*;
+
/// Errors of this crate
#[derive(Debug, Error)]
pub enum Error {
@@ -24,6 +28,10 @@ pub enum Error {
#[error(display = "Forbidden: {}", _0)]
Forbidden(String),
+ /// Authorization Header Malformed
+ #[error(display = "Authorization header malformed, expected scope: {}", _0)]
+ AuthorizationHeaderMalformed(String),
+
/// The object requested don't exists
#[error(display = "Not found")]
NotFound,
@@ -77,6 +85,29 @@ impl Error {
_ => StatusCode::BAD_REQUEST,
}
}
+
+ pub fn aws_code(&self) -> &'static str {
+ match self {
+ Error::NotFound => "NoSuchKey",
+ Error::Forbidden(_) => "AccessDenied",
+ Error::AuthorizationHeaderMalformed(_) => "AuthorizationHeaderMalformed",
+ Error::InternalError(GarageError::RPC(_)) => "ServiceUnavailable",
+ Error::InternalError(_) | Error::Hyper(_) | Error::HTTP(_) => "InternalError",
+ _ => "InvalidRequest",
+ }
+ }
+
+ 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
+ }
}
/// Trait to map error to the Bad Request error code