2020-09-15 15:10:41 +02:00
|
|
|
use super::{handle_error, into_response_helper, ResourceResult};
|
2020-05-01 14:48:11 +00:00
|
|
|
#[cfg(feature = "openapi")]
|
|
|
|
use crate::OpenapiSchema;
|
2020-09-15 15:10:41 +02:00
|
|
|
use crate::{result::ResourceError, Response, ResponseBody, StatusCode};
|
2020-05-01 14:48:11 +00:00
|
|
|
use futures_core::future::Future;
|
|
|
|
use mime::{Mime, APPLICATION_JSON};
|
2020-09-15 15:10:41 +02:00
|
|
|
use std::{error::Error, fmt::Display, pin::Pin};
|
|
|
|
|
|
|
|
pub trait IntoResponseError {
|
|
|
|
type Err: Error + Send + 'static;
|
2020-05-01 14:48:11 +00:00
|
|
|
|
|
|
|
fn into_response_error(self) -> Result<Response, Self::Err>;
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:10:41 +02:00
|
|
|
impl<E: Error> IntoResponseError for E {
|
2020-05-01 14:48:11 +00:00
|
|
|
type Err = serde_json::Error;
|
2020-09-15 15:10:41 +02:00
|
|
|
|
|
|
|
fn into_response_error(self) -> Result<Response, Self::Err> {
|
|
|
|
let err: ResourceError = self.into();
|
|
|
|
Ok(Response::json(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
serde_json::to_string(&err)?
|
|
|
|
))
|
2020-05-01 14:48:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R, E> ResourceResult for Result<R, E>
|
|
|
|
where
|
2020-09-15 15:10:41 +02:00
|
|
|
R: ResponseBody,
|
|
|
|
E: Display + IntoResponseError<Err = serde_json::Error>
|
2020-05-01 14:48:11 +00:00
|
|
|
{
|
|
|
|
type Err = E::Err;
|
2020-09-15 15:10:41 +02:00
|
|
|
|
|
|
|
fn into_response(self) -> Pin<Box<dyn Future<Output = Result<Response, E::Err>> + Send>> {
|
2020-05-01 14:48:11 +00:00
|
|
|
match self {
|
|
|
|
Ok(r) => into_response_helper(|| Ok(Response::json(StatusCode::OK, serde_json::to_string(&r)?))),
|
|
|
|
Err(e) => handle_error(e)
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 15:10:41 +02:00
|
|
|
|
|
|
|
fn accepted_types() -> Option<Vec<Mime>> {
|
2020-05-01 14:48:11 +00:00
|
|
|
Some(vec![APPLICATION_JSON])
|
|
|
|
}
|
2020-09-15 15:10:41 +02:00
|
|
|
|
2020-05-01 14:48:11 +00:00
|
|
|
#[cfg(feature = "openapi")]
|
2020-09-15 15:10:41 +02:00
|
|
|
fn schema() -> OpenapiSchema {
|
2020-05-01 14:48:11 +00:00
|
|
|
R::schema()
|
|
|
|
}
|
2020-05-03 18:48:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-09-15 15:10:41 +02:00
|
|
|
mod test {
|
2020-05-03 18:48:55 +02:00
|
|
|
use super::*;
|
|
|
|
use crate::result::OrAllTypes;
|
|
|
|
use futures_executor::block_on;
|
|
|
|
use thiserror::Error;
|
2020-09-15 15:10:41 +02:00
|
|
|
|
2020-05-03 18:48:55 +02:00
|
|
|
#[derive(Debug, Default, Deserialize, Serialize)]
|
|
|
|
#[cfg_attr(feature = "openapi", derive(crate::OpenapiType))]
|
2020-09-15 15:10:41 +02:00
|
|
|
struct Msg {
|
|
|
|
msg: String
|
2020-05-03 18:48:55 +02:00
|
|
|
}
|
2020-09-15 15:10:41 +02:00
|
|
|
|
2020-05-03 18:48:55 +02:00
|
|
|
#[derive(Debug, Default, Error)]
|
|
|
|
#[error("An Error")]
|
|
|
|
struct MsgError;
|
2020-09-15 15:10:41 +02:00
|
|
|
|
2020-05-03 18:48:55 +02:00
|
|
|
#[test]
|
2020-09-15 15:10:41 +02:00
|
|
|
fn result_ok() {
|
|
|
|
let ok: Result<Msg, MsgError> = Ok(Msg::default());
|
2020-05-03 18:48:55 +02:00
|
|
|
let res = block_on(ok.into_response()).expect("didn't expect error response");
|
|
|
|
assert_eq!(res.status, StatusCode::OK);
|
|
|
|
assert_eq!(res.mime, Some(APPLICATION_JSON));
|
2021-01-01 18:03:31 +01:00
|
|
|
assert_eq!(res.full_body().unwrap(), br#"{"msg":""}"#);
|
2020-05-03 18:48:55 +02:00
|
|
|
}
|
2020-09-15 15:10:41 +02:00
|
|
|
|
2020-05-03 18:48:55 +02:00
|
|
|
#[test]
|
2020-09-15 15:10:41 +02:00
|
|
|
fn result_err() {
|
|
|
|
let err: Result<Msg, MsgError> = Err(MsgError::default());
|
2020-05-03 18:48:55 +02:00
|
|
|
let res = block_on(err.into_response()).expect("didn't expect error response");
|
|
|
|
assert_eq!(res.status, StatusCode::INTERNAL_SERVER_ERROR);
|
|
|
|
assert_eq!(res.mime, Some(APPLICATION_JSON));
|
2020-09-15 15:10:41 +02:00
|
|
|
assert_eq!(
|
|
|
|
res.full_body().unwrap(),
|
|
|
|
format!(r#"{{"error":true,"message":"{}"}}"#, MsgError::default()).as_bytes()
|
|
|
|
);
|
2020-05-03 18:48:55 +02:00
|
|
|
}
|
2020-09-15 15:10:41 +02:00
|
|
|
|
2020-05-03 18:48:55 +02:00
|
|
|
#[test]
|
2020-09-15 15:10:41 +02:00
|
|
|
fn success_accepts_json() {
|
|
|
|
assert!(<Result<Msg, MsgError>>::accepted_types()
|
|
|
|
.or_all_types()
|
|
|
|
.contains(&APPLICATION_JSON))
|
2020-05-03 18:48:55 +02:00
|
|
|
}
|
|
|
|
}
|