If HttpResponse is already wrapping an `actix_web::Error`, return the underlying `actix_web::Error` instead of generating a dummy 500.

This commit is contained in:
LukeMathWalker 2021-05-02 22:38:50 +01:00
parent 3a0fb3f89e
commit d1d4aeadf4
3 changed files with 29 additions and 1 deletions

View File

@ -14,6 +14,9 @@
### Removed
* Stop re-exporting `http` crate's `HeaderMap` types in addition to ours. [#2171]
### Fixed
* Converting an `HttpResponse` to an `Error` return the underlying `Error` if `HttpResponse` was built using `HttpResponse::from_error`.
[#2171]: https://github.com/actix/actix-web/pull/2171

View File

@ -130,7 +130,11 @@ impl<T: ResponseError + 'static> From<T> for Error {
/// Convert Response to a Error
impl From<Response<Body>> for Error {
fn from(res: Response<Body>) -> Error {
InternalError::from_response("", res).into()
if res.error.is_some() {
res.error.unwrap()
} else {
InternalError::from_response("", res).into()
}
}
}

View File

@ -341,6 +341,7 @@ mod tests {
use super::*;
use crate::body::Body;
use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE};
use crate::ResponseError;
#[test]
fn test_debug() {
@ -352,6 +353,26 @@ mod tests {
assert!(dbg.contains("Response"));
}
#[test]
fn test_response_error_conversions() {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MyError;
impl std::fmt::Display for MyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "An error")
}
}
impl ResponseError for MyError {}
let error = MyError;
let response = Response::from_error(error.clone().into());
let actix_error: crate::Error = response.into();
assert_eq!(actix_error.as_error::<MyError>(), Some(&error));
}
#[test]
fn test_into_response() {
let resp: Response<Body> = "test".into();