1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-11 01:39:33 +00:00

improve responseerror trait docs

This commit is contained in:
Rob Ede 2021-04-19 21:12:52 +01:00
parent 2aa674c1fd
commit 427fe6bd82
No known key found for this signature in database
GPG key ID: 97C636207D3EF933
2 changed files with 11 additions and 10 deletions

View file

@ -17,12 +17,10 @@ use crate::{body::Body, helpers::Writer, Response, ResponseBuilder};
pub use http::Error as HttpError;
/// A specialized [`std::result::Result`]
/// for actix web operations
/// A specialized [`std::result::Result`] for Actix Web operations.
///
/// This typedef is generally used to avoid writing out
/// `actix_http::error::Error` directly and is otherwise a direct mapping to
/// `Result`.
/// This typedef is generally used to avoid writing out `actix_http::error::Error` directly and is
/// otherwise a direct mapping to `Result`.
pub type Result<T, E = Error> = std::result::Result<T, E>;
/// General purpose actix web error.
@ -51,18 +49,20 @@ impl Error {
}
}
/// Error that can be converted to `Response`
/// Errors that can generate responses.
pub trait ResponseError: fmt::Debug + fmt::Display {
/// Response's status code
/// Returns appropriate status code for error.
///
/// Internal server error is generated by default.
/// A 500 Internal Server Error is used by default. If [error_response](Self::error_response) is
/// also implemented and does not call `self.status_code()`, then this will not be used.
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
/// Create response for error
/// Creates full response for error.
///
/// Internal server error is generated by default.
/// By default, the generated response uses a 500 Internal Server Error status code, a
/// `Content-Type` of `text/plain`, and the body is set to `Self`'s `Display` impl.
fn error_response(&self) -> Response<Body> {
let mut resp = Response::new(self.status_code());
let mut buf = BytesMut::new();

View file

@ -41,6 +41,7 @@ pub fn write_content_length<B: BufMut>(n: u64, buf: &mut B) {
buf.put_slice(b"\r\n");
}
// TODO: bench why this is needed
pub(crate) struct Writer<'a, B>(pub &'a mut B);
impl<'a, B> io::Write for Writer<'a, B>