From 081706982ab9ab3d6d6b1d7e03bcd8c88c177367 Mon Sep 17 00:00:00 2001 From: Philip Jenvey Date: Thu, 7 May 2026 00:36:46 -0700 Subject: [PATCH] feat: add &mut versions of as_response_error/as_error/HttpResponse::error --- actix-web/src/error/error.rs | 12 +++++++++++- actix-web/src/response/response.rs | 8 +++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/actix-web/src/error/error.rs b/actix-web/src/error/error.rs index 670a58a00e7..8ecb9702980 100644 --- a/actix-web/src/error/error.rs +++ b/actix-web/src/error/error.rs @@ -17,16 +17,26 @@ pub struct Error { } impl Error { - /// Returns the reference to the underlying `ResponseError`. + /// Returns a reference to the underlying `ResponseError`. pub fn as_response_error(&self) -> &dyn ResponseError { self.cause.as_ref() } + /// Returns a mutable reference to the underlying `ResponseError`. + pub fn as_response_error_mut(&mut self) -> &mut dyn ResponseError { + self.cause.as_mut() + } + /// Similar to `as_response_error` but downcasts. pub fn as_error(&self) -> Option<&T> { ::downcast_ref(self.cause.as_ref()) } + /// Similar to `as_response_error_mut` but downcasts. + pub fn as_error_mut(&mut self) -> Option<&mut T> { + ::downcast_mut(self.cause.as_mut()) + } + /// Shortcut for creating an `HttpResponse`. pub fn error_response(&self) -> HttpResponse { self.cause.error_response() diff --git a/actix-web/src/response/response.rs b/actix-web/src/response/response.rs index e16dc0cd911..a3c2705eb4d 100644 --- a/actix-web/src/response/response.rs +++ b/actix-web/src/response/response.rs @@ -73,12 +73,18 @@ impl HttpResponse { self.res.head_mut() } - /// The source `error` for this response + /// Returns a reference to the source `error` for this response. #[inline] pub fn error(&self) -> Option<&Error> { self.error.as_ref() } + /// Returns a mutable reference to the source `error` for this response. + #[inline] + pub fn error_mut(&mut self) -> Option<&mut Error> { + self.error.as_mut() + } + /// Get the response status code #[inline] pub fn status(&self) -> StatusCode {