1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

Implement std::error::Error for our custom errors

For allowing a more ergonomic use and better integration on the
ecosystem, this adds the `std::error::Error` `impl` for our custom
errors.

We intent to drop this hand made code once `derive_more` finishes the
addition of the Error derive support[1]. Until that is available, we
need to live with that.

1. https://github.com/JelteF/derive_more/issues/92

Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
This commit is contained in:
Otavio Salvador 2020-03-17 22:51:06 -03:00
parent 52c5755d56
commit 146ae4da18
8 changed files with 54 additions and 0 deletions

View File

@ -1,5 +1,13 @@
# Changes # Changes
## [Unreleased]
### Changed
* Implement `std::error::Error` for our custom errors [#1422]
[#1422]: https://github.com/actix/actix-web/pull/1422
## [3.0.0-alpha.1] - 2020-03-11 ## [3.0.0-alpha.1] - 2020-03-11
### Added ### Added

View File

@ -1,5 +1,13 @@
# Changes # Changes
## [Unreleased]
### Changed
* Implement `std::error::Error` for our custom errors [#1422]
[#1422]: https://github.com/actix/actix-web/pull/1422
## [2.0.0-alpha.2] - 2020-03-07 ## [2.0.0-alpha.2] - 2020-03-07
### Changed ### Changed

View File

@ -55,6 +55,8 @@ pub enum ConnectError {
Io(io::Error), Io(io::Error),
} }
impl std::error::Error for ConnectError {}
impl From<actix_connect::ConnectError> for ConnectError { impl From<actix_connect::ConnectError> for ConnectError {
fn from(err: actix_connect::ConnectError) -> ConnectError { fn from(err: actix_connect::ConnectError) -> ConnectError {
match err { match err {
@ -86,6 +88,8 @@ pub enum InvalidUrl {
HttpError(http::Error), HttpError(http::Error),
} }
impl std::error::Error for InvalidUrl {}
/// A set of errors that can occur during request sending and response reading /// A set of errors that can occur during request sending and response reading
#[derive(Debug, Display, From)] #[derive(Debug, Display, From)]
pub enum SendRequestError { pub enum SendRequestError {
@ -115,6 +119,8 @@ pub enum SendRequestError {
Body(Error), Body(Error),
} }
impl std::error::Error for SendRequestError {}
/// Convert `SendRequestError` to a server `Response` /// Convert `SendRequestError` to a server `Response`
impl ResponseError for SendRequestError { impl ResponseError for SendRequestError {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {
@ -139,6 +145,8 @@ pub enum FreezeRequestError {
Http(HttpError), Http(HttpError),
} }
impl std::error::Error for FreezeRequestError {}
impl From<FreezeRequestError> for SendRequestError { impl From<FreezeRequestError> for SendRequestError {
fn from(e: FreezeRequestError) -> Self { fn from(e: FreezeRequestError) -> Self {
match e { match e {

View File

@ -333,6 +333,8 @@ pub enum PayloadError {
Io(io::Error), Io(io::Error),
} }
impl std::error::Error for PayloadError {}
impl From<h2::Error> for PayloadError { impl From<h2::Error> for PayloadError {
fn from(err: h2::Error) -> Self { fn from(err: h2::Error) -> Self {
PayloadError::Http2Payload(err) PayloadError::Http2Payload(err)
@ -441,6 +443,8 @@ pub enum ContentTypeError {
UnknownEncoding, UnknownEncoding,
} }
impl std::error::Error for ContentTypeError {}
/// Return `BadRequest` for `ContentTypeError` /// Return `BadRequest` for `ContentTypeError`
impl ResponseError for ContentTypeError { impl ResponseError for ContentTypeError {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {

View File

@ -58,6 +58,8 @@ pub enum ProtocolError {
Io(io::Error), Io(io::Error),
} }
impl std::error::Error for ProtocolError {}
impl ResponseError for ProtocolError {} impl ResponseError for ProtocolError {}
/// Websocket handshake errors /// Websocket handshake errors

View File

@ -1,5 +1,13 @@
# Changes # Changes
## [Unreleased]
### Changed
* Implement `std::error::Error` for our custom errors [#1422]
[#1422]: https://github.com/actix/actix-web/pull/1422
## [2.0.0-alpha.1] - 2020-03-11 ## [2.0.0-alpha.1] - 2020-03-11
* Update `actix-http` dependency to 2.0.0-alpha.2 * Update `actix-http` dependency to 2.0.0-alpha.2

View File

@ -42,6 +42,8 @@ pub enum WsClientError {
SendRequest(SendRequestError), SendRequest(SendRequestError),
} }
impl std::error::Error for WsClientError {}
impl From<InvalidUrl> for WsClientError { impl From<InvalidUrl> for WsClientError {
fn from(err: InvalidUrl) -> Self { fn from(err: InvalidUrl) -> Self {
WsClientError::SendRequest(err.into()) WsClientError::SendRequest(err.into())
@ -68,5 +70,7 @@ pub enum JsonPayloadError {
Payload(PayloadError), Payload(PayloadError),
} }
impl std::error::Error for JsonPayloadError {}
/// Return `InternalServerError` for `JsonPayloadError` /// Return `InternalServerError` for `JsonPayloadError`
impl ResponseError for JsonPayloadError {} impl ResponseError for JsonPayloadError {}

View File

@ -21,6 +21,8 @@ pub enum UrlGenerationError {
ParseError(UrlParseError), ParseError(UrlParseError),
} }
impl std::error::Error for UrlGenerationError {}
/// `InternalServerError` for `UrlGeneratorError` /// `InternalServerError` for `UrlGeneratorError`
impl ResponseError for UrlGenerationError {} impl ResponseError for UrlGenerationError {}
@ -51,6 +53,8 @@ pub enum UrlencodedError {
Payload(PayloadError), Payload(PayloadError),
} }
impl std::error::Error for UrlencodedError {}
/// Return `BadRequest` for `UrlencodedError` /// Return `BadRequest` for `UrlencodedError`
impl ResponseError for UrlencodedError { impl ResponseError for UrlencodedError {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {
@ -79,6 +83,8 @@ pub enum JsonPayloadError {
Payload(PayloadError), Payload(PayloadError),
} }
impl std::error::Error for JsonPayloadError {}
/// Return `BadRequest` for `JsonPayloadError` /// Return `BadRequest` for `JsonPayloadError`
impl ResponseError for JsonPayloadError { impl ResponseError for JsonPayloadError {
fn error_response(&self) -> HttpResponse { fn error_response(&self) -> HttpResponse {
@ -99,6 +105,8 @@ pub enum PathError {
Deserialize(serde::de::value::Error), Deserialize(serde::de::value::Error),
} }
impl std::error::Error for PathError {}
/// Return `BadRequest` for `PathError` /// Return `BadRequest` for `PathError`
impl ResponseError for PathError { impl ResponseError for PathError {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {
@ -114,6 +122,8 @@ pub enum QueryPayloadError {
Deserialize(serde::de::value::Error), Deserialize(serde::de::value::Error),
} }
impl std::error::Error for QueryPayloadError {}
/// Return `BadRequest` for `QueryPayloadError` /// Return `BadRequest` for `QueryPayloadError`
impl ResponseError for QueryPayloadError { impl ResponseError for QueryPayloadError {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {
@ -139,6 +149,8 @@ pub enum ReadlinesError {
ContentTypeError(ContentTypeError), ContentTypeError(ContentTypeError),
} }
impl std::error::Error for ReadlinesError {}
/// Return `BadRequest` for `ReadlinesError` /// Return `BadRequest` for `ReadlinesError`
impl ResponseError for ReadlinesError { impl ResponseError for ReadlinesError {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {