This commit is contained in:
FAIZAL KHAN 2026-05-31 23:16:52 +05:30 committed by GitHub
commit f1180a3de5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 109 additions and 3 deletions

View File

@ -8,6 +8,7 @@ use actix_http::{
header::HeaderMap,
Extensions, Response, ResponseHead, StatusCode,
};
use bytes::Bytes;
#[cfg(feature = "cookies")]
use {
actix_http::{
@ -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 {
@ -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
@ -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)]

View File

@ -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.
@ -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 {
@ -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![