Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions actix-web/src/response/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use actix_http::{
header::HeaderMap,
Extensions, Response, ResponseHead, StatusCode,
};
use bytes::Bytes;
#[cfg(feature = "cookies")]
use {
actix_http::{
Expand Down Expand Up @@ -79,6 +80,12 @@ impl<B> HttpResponse<B> {
self.error.as_ref()
}

/// Retrieve the source `error` for this response, leaving a `None` in its place.
#[inline]
pub fn take_error(&mut self) -> Option<Error> {
self.error.take()
}

/// Get the response status code
#[inline]
pub fn status(&self) -> StatusCode {
Expand Down Expand Up @@ -319,6 +326,78 @@ impl From<Error> for HttpResponse {
}
}

impl From<&'static str> for HttpResponse<&'static str> {
fn from(val: &'static str) -> Self {
HttpResponse {
res: Response::from(val),
error: None,
}
}
}

impl From<&'static [u8]> for HttpResponse<&'static [u8]> {
fn from(val: &'static [u8]) -> Self {
HttpResponse {
res: Response::from(val),
error: None,
}
}
}

impl From<String> for HttpResponse<String> {
fn from(val: String) -> Self {
HttpResponse {
res: Response::from(val),
error: None,
}
}
}

impl From<&String> for HttpResponse<String> {
fn from(val: &String) -> Self {
HttpResponse {
res: Response::from(val),
error: None,
}
}
}

impl From<Vec<u8>> for HttpResponse<Vec<u8>> {
fn from(val: Vec<u8>) -> Self {
HttpResponse {
res: Response::from(val),
error: None,
}
}
}

impl From<&Vec<u8>> for HttpResponse<Vec<u8>> {
fn from(val: &Vec<u8>) -> Self {
HttpResponse {
res: Response::from(val),
error: None,
}
}
}

impl From<Bytes> for HttpResponse<Bytes> {
fn from(val: Bytes) -> Self {
HttpResponse {
res: Response::from(val),
error: None,
}
}
}

impl From<bytestring::ByteString> for HttpResponse<bytestring::ByteString> {
fn from(val: bytestring::ByteString) -> Self {
HttpResponse {
res: Response::from(val),
error: None,
}
}
}

impl<B> From<HttpResponse<B>> for Response<B> {
fn from(res: HttpResponse<B>) -> Self {
// this impl will always be called as part of dispatcher
Expand Down Expand Up @@ -415,6 +494,17 @@ mod tests {
let dbg = format!("{:?}", resp);
assert!(dbg.contains("HttpResponse"));
}

#[test]
fn test_take_error() {
let err = crate::error::ErrorBadRequest("error");
let mut resp = HttpResponse::from_error(err);
assert!(resp.error().is_some());

let err = resp.take_error();
assert!(err.is_some());
assert!(resp.error().is_none());
}
}

#[cfg(test)]
Expand Down
22 changes: 19 additions & 3 deletions actix-web/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ impl ServiceRequest {

/// Create `ServiceResponse` from this request and given response.
#[inline]
pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
let res = HttpResponse::from(res.into());
ServiceResponse::new(self.req, res)
pub fn into_response<B, R: Into<HttpResponse<B>>>(self, res: R) -> ServiceResponse<B> {
ServiceResponse::new(self.req, res.into())
}

/// Create `ServiceResponse` from this request and given error.
Expand Down Expand Up @@ -463,6 +462,12 @@ impl<B> ServiceResponse<B> {
&mut self.response
}

/// Retrieve the source `error` for this response, leaving a `None` in its place.
#[inline]
pub fn take_error(&mut self) -> Option<Error> {
self.response.take_error()
}

/// Returns response status code.
#[inline]
pub fn status(&self) -> StatusCode {
Expand Down Expand Up @@ -803,6 +808,17 @@ mod tests {
assert!(s.contains("x-test"));
}

#[test]
fn test_take_error() {
let res = HttpResponse::from_error(crate::error::ErrorBadRequest("error"));
let mut res = TestRequest::default().to_srv_response(res);
assert!(res.response().error().is_some());

let err = res.take_error();
assert!(err.is_some());
assert!(res.response().error().is_none());
}

#[actix_rt::test]
async fn test_services_macro() {
let scoped = services![
Expand Down
Loading