1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

use Error instead of InternalError for helper methods error::ErrorXXX

This commit is contained in:
Nikolay Kim 2018-03-18 14:18:47 -07:00
parent e0c8da567c
commit ab73da4a1a
5 changed files with 53 additions and 28 deletions

View File

@ -2,7 +2,7 @@
## 0.4.10 (2018-03-xx) ## 0.4.10 (2018-03-xx)
.. * Use `Error` instead of `InternalError` for `error::ErrorXXXX` methods
## 0.4.9 (2018-03-16) ## 0.4.9 (2018-03-16)

View File

@ -191,7 +191,7 @@ impl<A, S> ActorHttpContext for HttpContext<A, S> where A: Actor<Context=Self>,
if self.inner.alive() { if self.inner.alive() {
match self.inner.poll(ctx) { match self.inner.poll(ctx) {
Ok(Async::NotReady) | Ok(Async::Ready(())) => (), Ok(Async::NotReady) | Ok(Async::Ready(())) => (),
Err(_) => return Err(ErrorInternalServerError("error").into()), Err(_) => return Err(ErrorInternalServerError("error")),
} }
} }

View File

@ -575,68 +575,91 @@ impl<T> Responder for InternalError<T>
/// Helper function that creates wrapper of any error and generate *BAD REQUEST* response. /// Helper function that creates wrapper of any error and generate *BAD REQUEST* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorBadRequest<T>(err: T) -> InternalError<T> { pub fn ErrorBadRequest<T>(err: T) -> Error
InternalError::new(err, StatusCode::BAD_REQUEST) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::BAD_REQUEST).into()
} }
/// Helper function that creates wrapper of any error and generate *UNAUTHORIZED* response. /// Helper function that creates wrapper of any error and generate *UNAUTHORIZED* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorUnauthorized<T>(err: T) -> InternalError<T> { pub fn ErrorUnauthorized<T>(err: T) -> Error
InternalError::new(err, StatusCode::UNAUTHORIZED) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::UNAUTHORIZED).into()
} }
/// Helper function that creates wrapper of any error and generate *FORBIDDEN* response. /// Helper function that creates wrapper of any error and generate *FORBIDDEN* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorForbidden<T>(err: T) -> InternalError<T> { pub fn ErrorForbidden<T>(err: T) -> Error
InternalError::new(err, StatusCode::FORBIDDEN) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::FORBIDDEN).into()
} }
/// Helper function that creates wrapper of any error and generate *NOT FOUND* response. /// Helper function that creates wrapper of any error and generate *NOT FOUND* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorNotFound<T>(err: T) -> InternalError<T> { pub fn ErrorNotFound<T>(err: T) -> Error
InternalError::new(err, StatusCode::NOT_FOUND) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::NOT_FOUND).into()
} }
/// Helper function that creates wrapper of any error and generate *METHOD NOT ALLOWED* response. /// Helper function that creates wrapper of any error and generate *METHOD NOT ALLOWED* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorMethodNotAllowed<T>(err: T) -> InternalError<T> { pub fn ErrorMethodNotAllowed<T>(err: T) -> Error
InternalError::new(err, StatusCode::METHOD_NOT_ALLOWED) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::METHOD_NOT_ALLOWED).into()
} }
/// Helper function that creates wrapper of any error and generate *REQUEST TIMEOUT* response. /// Helper function that creates wrapper of any error and generate *REQUEST TIMEOUT* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorRequestTimeout<T>(err: T) -> InternalError<T> { pub fn ErrorRequestTimeout<T>(err: T) -> Error
InternalError::new(err, StatusCode::REQUEST_TIMEOUT) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::REQUEST_TIMEOUT).into()
} }
/// Helper function that creates wrapper of any error and generate *CONFLICT* response. /// Helper function that creates wrapper of any error and generate *CONFLICT* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorConflict<T>(err: T) -> InternalError<T> { pub fn ErrorConflict<T>(err: T) -> Error
InternalError::new(err, StatusCode::CONFLICT) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::CONFLICT).into()
} }
/// Helper function that creates wrapper of any error and generate *GONE* response. /// Helper function that creates wrapper of any error and generate *GONE* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorGone<T>(err: T) -> InternalError<T> { pub fn ErrorGone<T>(err: T) -> Error
InternalError::new(err, StatusCode::GONE) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::GONE).into()
} }
/// Helper function that creates wrapper of any error and generate *PRECONDITION FAILED* response. /// Helper function that creates wrapper of any error and generate *PRECONDITION FAILED* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorPreconditionFailed<T>(err: T) -> InternalError<T> { pub fn ErrorPreconditionFailed<T>(err: T) -> Error
InternalError::new(err, StatusCode::PRECONDITION_FAILED) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::PRECONDITION_FAILED).into()
} }
/// Helper function that creates wrapper of any error and generate *EXPECTATION FAILED* response. /// Helper function that creates wrapper of any error and generate *EXPECTATION FAILED* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorExpectationFailed<T>(err: T) -> InternalError<T> { pub fn ErrorExpectationFailed<T>(err: T) -> Error
InternalError::new(err, StatusCode::EXPECTATION_FAILED) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::EXPECTATION_FAILED).into()
} }
/// Helper function that creates wrapper of any error and generate *INTERNAL SERVER ERROR* response. /// Helper function that creates wrapper of any error and
/// generate *INTERNAL SERVER ERROR* response.
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn ErrorInternalServerError<T>(err: T) -> InternalError<T> { pub fn ErrorInternalServerError<T>(err: T) -> Error
InternalError::new(err, StatusCode::INTERNAL_SERVER_ERROR) where T: Send + Sync + fmt::Debug + 'static
{
InternalError::new(err, StatusCode::INTERNAL_SERVER_ERROR).into()
} }
#[cfg(test)] #[cfg(test)]

View File

@ -4,9 +4,10 @@ use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use std::slice::Iter; use std::slice::Iter;
use std::borrow::Cow; use std::borrow::Cow;
use http::StatusCode;
use smallvec::SmallVec; use smallvec::SmallVec;
use error::{ResponseError, UriSegmentError, InternalError, ErrorBadRequest}; use error::{ResponseError, UriSegmentError, InternalError};
/// A trait to abstract the idea of creating a new instance of a type from a path parameter. /// A trait to abstract the idea of creating a new instance of a type from a path parameter.
@ -144,7 +145,8 @@ macro_rules! FROM_STR {
type Err = InternalError<<$type as FromStr>::Err>; type Err = InternalError<<$type as FromStr>::Err>;
fn from_param(val: &str) -> Result<Self, Self::Err> { fn from_param(val: &str) -> Result<Self, Self::Err> {
<$type as FromStr>::from_str(val).map_err(ErrorBadRequest) <$type as FromStr>::from_str(val)
.map_err(|e| InternalError::new(e, StatusCode::BAD_REQUEST))
} }
} }
} }

View File

@ -205,7 +205,7 @@ impl<A, S> ActorHttpContext for WebsocketContext<A, S> where A: Actor<Context=Se
}; };
if self.inner.alive() && self.inner.poll(ctx).is_err() { if self.inner.alive() && self.inner.poll(ctx).is_err() {
return Err(ErrorInternalServerError("error").into()) return Err(ErrorInternalServerError("error"))
} }
// frames // frames