feat: add &mut versions of as_response_error/as_error/HttpResponse::error

This commit is contained in:
Philip Jenvey 2026-05-07 00:36:46 -07:00
parent e2db4980cf
commit 081706982a
No known key found for this signature in database
GPG Key ID: 5B9F83DE4F7EB7FA
2 changed files with 18 additions and 2 deletions

View File

@ -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<T: ResponseError + 'static>(&self) -> Option<&T> {
<dyn ResponseError>::downcast_ref(self.cause.as_ref())
}
/// Similar to `as_response_error_mut` but downcasts.
pub fn as_error_mut<T: ResponseError + 'static>(&mut self) -> Option<&mut T> {
<dyn ResponseError>::downcast_mut(self.cause.as_mut())
}
/// Shortcut for creating an `HttpResponse`.
pub fn error_response(&self) -> HttpResponse {
self.cause.error_response()

View File

@ -73,12 +73,18 @@ impl<B> HttpResponse<B> {
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 {