use crate::protocols::Protocol;
#[derive(Debug)]
pub enum RuntimeErrorKind {
Serialization(crate::Error),
InternalFailure(crate::Error),
}
impl RuntimeErrorKind {
pub fn name(&self) -> &'static str {
match self {
RuntimeErrorKind::Serialization(_) => "SerializationException",
RuntimeErrorKind::InternalFailure(_) => "InternalFailureException",
}
}
}
#[derive(Debug)]
pub struct RuntimeError {
pub protocol: Protocol,
pub kind: RuntimeErrorKind,
}
impl axum_core::response::IntoResponse for RuntimeError {
fn into_response(self) -> axum_core::response::Response {
let status_code = match self.kind {
RuntimeErrorKind::Serialization(_) => http::StatusCode::BAD_REQUEST,
RuntimeErrorKind::InternalFailure(_) => http::StatusCode::INTERNAL_SERVER_ERROR,
};
let body = crate::body::to_boxed(match self.protocol {
Protocol::RestJson1 => "{}",
Protocol::RestXml => "",
});
let mut builder = http::Response::builder();
builder = builder.status(status_code);
match self.protocol {
Protocol::RestJson1 => {
builder = builder
.header("Content-Type", "application/json")
.header("X-Amzn-Errortype", self.kind.name());
}
Protocol::RestXml => {
builder = builder.header("Content-Type", "application/xml");
}
}
builder = builder.extension(crate::extension::RuntimeErrorExtension::new(String::from(
self.kind.name(),
)));
builder.body(body).expect("invalid HTTP response for `RuntimeError`; please file a bug report under https://github.com/awslabs/smithy-rs/issues")
}
}
impl From<crate::rejection::RequestExtensionNotFoundRejection> for RuntimeErrorKind {
fn from(err: crate::rejection::RequestExtensionNotFoundRejection) -> Self {
RuntimeErrorKind::InternalFailure(crate::Error::new(err))
}
}
impl From<crate::rejection::ResponseRejection> for RuntimeErrorKind {
fn from(err: crate::rejection::ResponseRejection) -> Self {
RuntimeErrorKind::Serialization(crate::Error::new(err))
}
}
impl From<crate::rejection::RequestRejection> for RuntimeErrorKind {
fn from(err: crate::rejection::RequestRejection) -> Self {
RuntimeErrorKind::Serialization(crate::Error::new(err))
}
}