Possibility of different error types for different APIs

This commit is contained in:
Alex 2022-05-13 13:51:34 +02:00
parent e4e1f8f0d6
commit 983037d965
Signed by: lx
GPG key ID: 0E496D15096376BE
7 changed files with 64 additions and 50 deletions

View file

@ -96,6 +96,7 @@ impl ApiHandler for AdminApiServer {
const API_NAME_DISPLAY: &'static str = "Admin"; const API_NAME_DISPLAY: &'static str = "Admin";
type Endpoint = Endpoint; type Endpoint = Endpoint;
type Error = Error;
fn parse_endpoint(&self, req: &Request<Body>) -> Result<Endpoint, Error> { fn parse_endpoint(&self, req: &Request<Body>) -> Result<Endpoint, Error> {
Endpoint::from_request(req) Endpoint::from_request(req)

View file

@ -2,11 +2,12 @@ use std::convert::TryInto;
use err_derive::Error; use err_derive::Error;
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use hyper::{HeaderMap, StatusCode}; use hyper::{Body, HeaderMap, StatusCode};
use garage_model::helper::error::Error as HelperError; use garage_model::helper::error::Error as HelperError;
use garage_util::error::Error as GarageError; use garage_util::error::Error as GarageError;
use crate::generic_server::ApiError;
use crate::s3::xml as s3_xml; use crate::s3::xml as s3_xml;
/// Errors of this crate /// Errors of this crate
@ -142,28 +143,6 @@ impl From<multer::Error> for Error {
} }
impl Error { impl Error {
/// Get the HTTP status code that best represents the meaning of the error for the client
pub fn http_status_code(&self) -> StatusCode {
match self {
Error::NoSuchKey | Error::NoSuchBucket | Error::NoSuchUpload => StatusCode::NOT_FOUND,
Error::BucketNotEmpty | Error::BucketAlreadyExists => StatusCode::CONFLICT,
Error::PreconditionFailed => StatusCode::PRECONDITION_FAILED,
Error::Forbidden(_) => StatusCode::FORBIDDEN,
Error::NotAcceptable(_) => StatusCode::NOT_ACCEPTABLE,
Error::InternalError(
GarageError::Timeout
| GarageError::RemoteError(_)
| GarageError::Quorum(_, _, _, _),
) => StatusCode::SERVICE_UNAVAILABLE,
Error::InternalError(_) | Error::Hyper(_) | Error::Http(_) => {
StatusCode::INTERNAL_SERVER_ERROR
}
Error::InvalidRange(_) => StatusCode::RANGE_NOT_SATISFIABLE,
Error::NotImplemented(_) => StatusCode::NOT_IMPLEMENTED,
_ => StatusCode::BAD_REQUEST,
}
}
pub fn aws_code(&self) -> &'static str { pub fn aws_code(&self) -> &'static str {
match self { match self {
Error::NoSuchKey => "NoSuchKey", Error::NoSuchKey => "NoSuchKey",
@ -187,27 +166,32 @@ impl Error {
_ => "InvalidRequest", _ => "InvalidRequest",
} }
} }
}
pub fn aws_xml(&self, garage_region: &str, path: &str) -> String { impl ApiError for Error {
let error = s3_xml::Error { /// Get the HTTP status code that best represents the meaning of the error for the client
code: s3_xml::Value(self.aws_code().to_string()), fn http_status_code(&self) -> StatusCode {
message: s3_xml::Value(format!("{}", self)), match self {
resource: Some(s3_xml::Value(path.to_string())), Error::NoSuchKey | Error::NoSuchBucket | Error::NoSuchUpload => StatusCode::NOT_FOUND,
region: Some(s3_xml::Value(garage_region.to_string())), Error::BucketNotEmpty | Error::BucketAlreadyExists => StatusCode::CONFLICT,
}; Error::PreconditionFailed => StatusCode::PRECONDITION_FAILED,
s3_xml::to_xml_with_header(&error).unwrap_or_else(|_| { Error::Forbidden(_) => StatusCode::FORBIDDEN,
r#" Error::NotAcceptable(_) => StatusCode::NOT_ACCEPTABLE,
<?xml version="1.0" encoding="UTF-8"?> Error::InternalError(
<Error> GarageError::Timeout
<Code>InternalError</Code> | GarageError::RemoteError(_)
<Message>XML encoding of error failed</Message> | GarageError::Quorum(_, _, _, _),
</Error> ) => StatusCode::SERVICE_UNAVAILABLE,
"# Error::InternalError(_) | Error::Hyper(_) | Error::Http(_) => {
.into() StatusCode::INTERNAL_SERVER_ERROR
}) }
Error::InvalidRange(_) => StatusCode::RANGE_NOT_SATISFIABLE,
Error::NotImplemented(_) => StatusCode::NOT_IMPLEMENTED,
_ => StatusCode::BAD_REQUEST,
}
} }
pub fn add_headers(&self, header_map: &mut HeaderMap<HeaderValue>) { fn add_http_headers(&self, header_map: &mut HeaderMap<HeaderValue>) {
use hyper::header; use hyper::header;
#[allow(clippy::single_match)] #[allow(clippy::single_match)]
match self { match self {
@ -222,6 +206,25 @@ impl Error {
_ => (), _ => (),
} }
} }
fn http_body(&self, garage_region: &str, path: &str) -> Body {
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())),
};
Body::from(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()
}))
}
} }
/// Trait to map error to the Bad Request error code /// Trait to map error to the Bad Request error code

View file

@ -5,9 +5,11 @@ use async_trait::async_trait;
use futures::future::Future; use futures::future::Future;
use hyper::header::HeaderValue;
use hyper::server::conn::AddrStream; use hyper::server::conn::AddrStream;
use hyper::service::{make_service_fn, service_fn}; use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server}; use hyper::{Body, Request, Response, Server};
use hyper::{HeaderMap, StatusCode};
use opentelemetry::{ use opentelemetry::{
global, global,
@ -19,26 +21,31 @@ use opentelemetry::{
use garage_util::error::Error as GarageError; use garage_util::error::Error as GarageError;
use garage_util::metrics::{gen_trace_id, RecordDuration}; use garage_util::metrics::{gen_trace_id, RecordDuration};
use crate::error::*;
pub(crate) trait ApiEndpoint: Send + Sync + 'static { pub(crate) trait ApiEndpoint: Send + Sync + 'static {
fn name(&self) -> &'static str; fn name(&self) -> &'static str;
fn add_span_attributes(&self, span: SpanRef<'_>); fn add_span_attributes(&self, span: SpanRef<'_>);
} }
pub trait ApiError: std::error::Error + Send + Sync + 'static {
fn http_status_code(&self) -> StatusCode;
fn add_http_headers(&self, header_map: &mut HeaderMap<HeaderValue>);
fn http_body(&self, garage_region: &str, path: &str) -> Body;
}
#[async_trait] #[async_trait]
pub(crate) trait ApiHandler: Send + Sync + 'static { pub(crate) trait ApiHandler: Send + Sync + 'static {
const API_NAME: &'static str; const API_NAME: &'static str;
const API_NAME_DISPLAY: &'static str; const API_NAME_DISPLAY: &'static str;
type Endpoint: ApiEndpoint; type Endpoint: ApiEndpoint;
type Error: ApiError;
fn parse_endpoint(&self, r: &Request<Body>) -> Result<Self::Endpoint, Error>; fn parse_endpoint(&self, r: &Request<Body>) -> Result<Self::Endpoint, Self::Error>;
async fn handle( async fn handle(
&self, &self,
req: Request<Body>, req: Request<Body>,
endpoint: Self::Endpoint, endpoint: Self::Endpoint,
) -> Result<Response<Body>, Error>; ) -> Result<Response<Body>, Self::Error>;
} }
pub(crate) struct ApiServer<A: ApiHandler> { pub(crate) struct ApiServer<A: ApiHandler> {
@ -142,13 +149,13 @@ impl<A: ApiHandler> ApiServer<A> {
Ok(x) Ok(x)
} }
Err(e) => { Err(e) => {
let body: Body = Body::from(e.aws_xml(&self.region, uri.path())); let body: Body = e.http_body(&self.region, uri.path());
let mut http_error_builder = Response::builder() let mut http_error_builder = Response::builder()
.status(e.http_status_code()) .status(e.http_status_code())
.header("Content-Type", "application/xml"); .header("Content-Type", "application/xml");
if let Some(header_map) = http_error_builder.headers_mut() { if let Some(header_map) = http_error_builder.headers_mut() {
e.add_headers(header_map) e.add_http_headers(header_map)
} }
let http_error = http_error_builder.body(body)?; let http_error = http_error_builder.body(body)?;
@ -163,7 +170,7 @@ impl<A: ApiHandler> ApiServer<A> {
} }
} }
async fn handler_stage2(&self, req: Request<Body>) -> Result<Response<Body>, Error> { async fn handler_stage2(&self, req: Request<Body>) -> Result<Response<Body>, A::Error> {
let endpoint = self.api_handler.parse_endpoint(&req)?; let endpoint = self.api_handler.parse_endpoint(&req)?;
debug!("Endpoint: {}", endpoint.name()); debug!("Endpoint: {}", endpoint.name());

View file

@ -60,6 +60,7 @@ impl ApiHandler for K2VApiServer {
const API_NAME_DISPLAY: &'static str = "K2V"; const API_NAME_DISPLAY: &'static str = "K2V";
type Endpoint = K2VApiEndpoint; type Endpoint = K2VApiEndpoint;
type Error = Error;
fn parse_endpoint(&self, req: &Request<Body>) -> Result<K2VApiEndpoint, Error> { fn parse_endpoint(&self, req: &Request<Body>) -> Result<K2VApiEndpoint, Error> {
let (endpoint, bucket_name) = Endpoint::from_request(req)?; let (endpoint, bucket_name) = Endpoint::from_request(req)?;

View file

@ -6,7 +6,7 @@ pub mod error;
pub use error::Error; pub use error::Error;
mod encoding; mod encoding;
mod generic_server; pub mod generic_server;
pub mod helpers; pub mod helpers;
mod router_macros; mod router_macros;
/// This mode is public only to help testing. Don't expect stability here /// This mode is public only to help testing. Don't expect stability here

View file

@ -75,6 +75,7 @@ impl ApiHandler for S3ApiServer {
const API_NAME_DISPLAY: &'static str = "S3"; const API_NAME_DISPLAY: &'static str = "S3";
type Endpoint = S3ApiEndpoint; type Endpoint = S3ApiEndpoint;
type Error = Error;
fn parse_endpoint(&self, req: &Request<Body>) -> Result<S3ApiEndpoint, Error> { fn parse_endpoint(&self, req: &Request<Body>) -> Result<S3ApiEndpoint, Error> {
let authority = req let authority = req

View file

@ -2,6 +2,7 @@ use err_derive::Error;
use hyper::header::HeaderValue; use hyper::header::HeaderValue;
use hyper::{HeaderMap, StatusCode}; use hyper::{HeaderMap, StatusCode};
use garage_api::generic_server::ApiError;
use garage_util::error::Error as GarageError; use garage_util::error::Error as GarageError;
/// Errors of this crate /// Errors of this crate
@ -52,7 +53,7 @@ impl Error {
pub fn add_headers(&self, header_map: &mut HeaderMap<HeaderValue>) { pub fn add_headers(&self, header_map: &mut HeaderMap<HeaderValue>) {
#[allow(clippy::single_match)] #[allow(clippy::single_match)]
match self { match self {
Error::ApiError(e) => e.add_headers(header_map), Error::ApiError(e) => e.add_http_headers(header_map),
_ => (), _ => (),
} }
} }