mirror of
https://github.com/actix/actix-web.git
synced 2025-01-18 13:15:27 +00:00
improve responseerror trait docs
This commit is contained in:
parent
2aa674c1fd
commit
427fe6bd82
2 changed files with 11 additions and 10 deletions
|
@ -17,12 +17,10 @@ use crate::{body::Body, helpers::Writer, Response, ResponseBuilder};
|
||||||
|
|
||||||
pub use http::Error as HttpError;
|
pub use http::Error as HttpError;
|
||||||
|
|
||||||
/// A specialized [`std::result::Result`]
|
/// A specialized [`std::result::Result`] for Actix Web operations.
|
||||||
/// for actix web operations
|
|
||||||
///
|
///
|
||||||
/// This typedef is generally used to avoid writing out
|
/// This typedef is generally used to avoid writing out `actix_http::error::Error` directly and is
|
||||||
/// `actix_http::error::Error` directly and is otherwise a direct mapping to
|
/// otherwise a direct mapping to `Result`.
|
||||||
/// `Result`.
|
|
||||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||||
|
|
||||||
/// General purpose actix web error.
|
/// 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 {
|
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 {
|
fn status_code(&self) -> StatusCode {
|
||||||
StatusCode::INTERNAL_SERVER_ERROR
|
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> {
|
fn error_response(&self) -> Response<Body> {
|
||||||
let mut resp = Response::new(self.status_code());
|
let mut resp = Response::new(self.status_code());
|
||||||
let mut buf = BytesMut::new();
|
let mut buf = BytesMut::new();
|
||||||
|
|
|
@ -41,6 +41,7 @@ pub fn write_content_length<B: BufMut>(n: u64, buf: &mut B) {
|
||||||
buf.put_slice(b"\r\n");
|
buf.put_slice(b"\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: bench why this is needed
|
||||||
pub(crate) struct Writer<'a, B>(pub &'a mut B);
|
pub(crate) struct Writer<'a, B>(pub &'a mut B);
|
||||||
|
|
||||||
impl<'a, B> io::Write for Writer<'a, B>
|
impl<'a, B> io::Write for Writer<'a, B>
|
||||||
|
|
Loading…
Reference in a new issue