use std::sync::Arc;
use actix_web::HttpResponse;
use arc_swap::ArcSwap;
use lazy_static::lazy_static;
#[allow(unused)]
pub trait ResponseTransform {
fn transform(
&self,
name: &str,
err: &dyn std::error::Error,
status_code: actix_web::http::StatusCode,
reason: Option<serde_json::Value>,
_type: Option<String>,
details: Option<serde_json::Value>,
) -> HttpResponse {
actix_web::HttpResponse::build(status_code).finish()
}
fn default_error_status_code(&self) -> actix_web::http::StatusCode {
actix_web::http::StatusCode::INTERNAL_SERVER_ERROR
}
}
struct ReflexiveTransform;
impl ResponseTransform for ReflexiveTransform {}
lazy_static! {
static ref RESPONSE_TRANSFORM: ArcSwap<Box<dyn ResponseTransform + Sync + Send>> =
ArcSwap::from(Arc::new(Box::new(ReflexiveTransform {}) as _));
}
pub fn set_global_transform(transform: impl ResponseTransform + Sync + Send + 'static) {
RESPONSE_TRANSFORM.swap(Arc::new(Box::new(transform)));
}
#[doc(hidden)]
pub fn apply_global_transform(
name: &str,
err: &dyn std::error::Error,
status_code: actix_web::http::StatusCode,
reason: Option<serde_json::Value>,
_type: Option<String>,
details: Option<serde_json::Value>,
) -> HttpResponse {
ResponseTransform::transform(
(**RESPONSE_TRANSFORM.load()).as_ref(),
name,
err,
status_code,
reason,
_type,
details,
)
}
#[doc(hidden)]
pub fn default_global_error_status_code() -> actix_web::http::StatusCode {
ResponseTransform::default_error_status_code((**RESPONSE_TRANSFORM.load()).as_ref())
}
#[doc(hidden)]
pub trait ThiserrorResponse {
fn status_code(&self) -> Option<actix_web::http::StatusCode> {
None
}
fn reason(&self) -> Option<Option<serde_json::Value>> {
None
}
fn _type(&self) -> Option<Option<String>> {
None
}
fn details(&self) -> Option<Option<serde_json::Value>> {
None
}
}
#[allow(unused_imports)]
#[macro_use]
extern crate actix_web_thiserror_derive;
pub use actix_web_thiserror_derive::ResponseError;