2024-05-04 16:34:26 +00:00
|
|
|
use actix_web::{error, Error, HttpRequest, HttpResponse, HttpResponseBuilder};
|
2021-11-08 12:11:20 +00:00
|
|
|
use serde::ser::SerializeStruct;
|
|
|
|
use serde::{Serialize, Serializer};
|
2024-05-04 16:34:26 +00:00
|
|
|
use serde_json::json;
|
2021-11-08 12:11:20 +00:00
|
|
|
|
2021-11-04 22:37:16 +00:00
|
|
|
pub mod v1;
|
2021-11-08 12:11:20 +00:00
|
|
|
|
|
|
|
enum EmgauwaJsonPayLoadError {
|
2022-07-19 22:38:09 +00:00
|
|
|
Error(error::JsonPayloadError),
|
2021-11-08 12:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for EmgauwaJsonPayLoadError {
|
2022-07-19 22:38:09 +00:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let mut s = serializer.serialize_struct("error", 3)?;
|
|
|
|
s.serialize_field("type", "json-payload-error")?;
|
|
|
|
s.serialize_field("code", &400)?;
|
|
|
|
s.serialize_field(
|
|
|
|
"description",
|
|
|
|
&match self {
|
|
|
|
EmgauwaJsonPayLoadError::Error(err) => format!("{}", err),
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
s.end()
|
|
|
|
}
|
2021-11-08 12:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn json_error_handler(err: error::JsonPayloadError, _: &HttpRequest) -> Error {
|
2022-07-19 22:38:09 +00:00
|
|
|
error::InternalError::from_response(
|
|
|
|
"",
|
|
|
|
HttpResponse::BadRequest()
|
|
|
|
.content_type("application/json")
|
|
|
|
.json(EmgauwaJsonPayLoadError::Error(err)),
|
|
|
|
)
|
|
|
|
.into()
|
2021-11-08 12:11:20 +00:00
|
|
|
}
|
2024-05-04 16:34:26 +00:00
|
|
|
|
|
|
|
pub trait EmgauwaMessage {
|
|
|
|
fn emgauwa_message(self, message: &str) -> HttpResponse;
|
|
|
|
}
|
|
|
|
impl EmgauwaMessage for HttpResponseBuilder {
|
|
|
|
fn emgauwa_message(mut self, message: &str) -> HttpResponse {
|
|
|
|
self.json(json!({ "message": message }))
|
|
|
|
}
|
|
|
|
}
|