mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-01-09 11:41:32 +00:00
fa4825b524
* Removing a few expects from production and test code. - Fixes #5192 * Using if let filter for admin emails. * Fixing unused error. * Adding expect_used = deny to clippy lints. * Update src/lib.rs Co-authored-by: Nutomic <me@nutomic.com> * Update crates/utils/src/settings/structs.rs Co-authored-by: Nutomic <me@nutomic.com> * Update crates/utils/src/settings/mod.rs Co-authored-by: Nutomic <me@nutomic.com> * Some more cleanup. * Fix clippy --------- Co-authored-by: Nutomic <me@nutomic.com>
122 lines
3.6 KiB
Rust
122 lines
3.6 KiB
Rust
use crate::error::{LemmyError, LemmyErrorType};
|
|
use actix_web::{dev::ServiceResponse, middleware::ErrorHandlerResponse, HttpResponse};
|
|
|
|
pub fn jsonify_plain_text_errors<BODY>(
|
|
res: ServiceResponse<BODY>,
|
|
) -> actix_web::Result<ErrorHandlerResponse<BODY>> {
|
|
let maybe_error = res.response().error();
|
|
|
|
// This function is only expected to be called for errors, so if there is no error, return
|
|
if maybe_error.is_none() {
|
|
return Ok(ErrorHandlerResponse::Response(res.map_into_left_body()));
|
|
}
|
|
// We're assuming that any LemmyError is already in JSON format, so we don't need to do anything
|
|
if let Some(maybe_error) = maybe_error {
|
|
if maybe_error.as_error::<LemmyError>().is_some() {
|
|
return Ok(ErrorHandlerResponse::Response(res.map_into_left_body()));
|
|
}
|
|
}
|
|
|
|
let (req, res_parts) = res.into_parts();
|
|
let lemmy_err_type = if let Some(error) = res_parts.error() {
|
|
LemmyErrorType::Unknown(error.to_string())
|
|
} else {
|
|
LemmyErrorType::Unknown("couldnt build json".into())
|
|
};
|
|
|
|
let response = HttpResponse::build(res_parts.status()).json(lemmy_err_type);
|
|
|
|
let service_response = ServiceResponse::new(req, response);
|
|
Ok(ErrorHandlerResponse::Response(
|
|
service_response.map_into_right_body(),
|
|
))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::error::{LemmyError, LemmyErrorType};
|
|
use actix_web::{
|
|
error::ErrorInternalServerError,
|
|
http::StatusCode,
|
|
middleware::ErrorHandlers,
|
|
test,
|
|
web,
|
|
App,
|
|
Error,
|
|
Handler,
|
|
Responder,
|
|
};
|
|
use pretty_assertions::assert_eq;
|
|
|
|
#[actix_web::test]
|
|
async fn test_non_error_responses_are_not_modified() {
|
|
async fn ok_service() -> actix_web::Result<String, Error> {
|
|
Ok("Oll Korrect".to_string())
|
|
}
|
|
|
|
check_for_jsonification(ok_service, StatusCode::OK, "Oll Korrect").await;
|
|
}
|
|
|
|
#[actix_web::test]
|
|
async fn test_lemmy_errors_are_not_modified() {
|
|
async fn lemmy_error_service() -> actix_web::Result<String, LemmyError> {
|
|
Err(LemmyError::from(LemmyErrorType::EmailAlreadyExists))
|
|
}
|
|
|
|
check_for_jsonification(
|
|
lemmy_error_service,
|
|
StatusCode::BAD_REQUEST,
|
|
"{\"error\":\"email_already_exists\"}",
|
|
)
|
|
.await;
|
|
}
|
|
|
|
#[actix_web::test]
|
|
async fn test_generic_errors_are_jsonified_as_unknown_errors() {
|
|
async fn generic_error_service() -> actix_web::Result<String, Error> {
|
|
Err(ErrorInternalServerError("This is not a LemmyError"))
|
|
}
|
|
|
|
check_for_jsonification(
|
|
generic_error_service,
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
"{\"error\":\"unknown\",\"message\":\"This is not a LemmyError\"}",
|
|
)
|
|
.await;
|
|
}
|
|
|
|
#[actix_web::test]
|
|
async fn test_anyhow_errors_wrapped_in_lemmy_errors_are_jsonified_correctly() {
|
|
async fn anyhow_error_service() -> actix_web::Result<String, LemmyError> {
|
|
Err(LemmyError::from(anyhow::anyhow!("This is the inner error")))
|
|
}
|
|
|
|
check_for_jsonification(
|
|
anyhow_error_service,
|
|
StatusCode::BAD_REQUEST,
|
|
"{\"error\":\"unknown\",\"message\":\"This is the inner error\"}",
|
|
)
|
|
.await;
|
|
}
|
|
|
|
async fn check_for_jsonification(
|
|
service: impl Handler<(), Output = impl Responder + 'static>,
|
|
expected_status_code: StatusCode,
|
|
expected_body: &str,
|
|
) {
|
|
let app = test::init_service(
|
|
App::new()
|
|
.wrap(ErrorHandlers::new().default_handler(jsonify_plain_text_errors))
|
|
.route("/", web::get().to(service)),
|
|
)
|
|
.await;
|
|
let req = test::TestRequest::default().to_request();
|
|
let res = test::call_service(&app, req).await;
|
|
|
|
assert_eq!(res.status(), expected_status_code);
|
|
|
|
let body = test::read_body(res).await;
|
|
assert_eq!(body, expected_body);
|
|
}
|
|
}
|