mirror of https://github.com/fafhrd91/actix-web
web: Convert from `derive_more` to `thiserror`
The `thiserror` has the advantage of implementing `std::error::Error` and it integrates better with the Rust ecosystem. Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
This commit is contained in:
parent
496626929d
commit
40ae556ec3
|
@ -75,7 +75,6 @@ actix-http = "2.0.0-alpha.2"
|
||||||
awc = { version = "2.0.0-alpha.1", default-features = false }
|
awc = { version = "2.0.0-alpha.1", default-features = false }
|
||||||
|
|
||||||
bytes = "0.5.3"
|
bytes = "0.5.3"
|
||||||
derive_more = "0.99.2"
|
|
||||||
encoding_rs = "0.8"
|
encoding_rs = "0.8"
|
||||||
futures = "0.3.1"
|
futures = "0.3.1"
|
||||||
fxhash = "0.2.1"
|
fxhash = "0.2.1"
|
||||||
|
@ -88,6 +87,7 @@ serde = { version = "1.0", features=["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_urlencoded = "0.6.1"
|
serde_urlencoded = "0.6.1"
|
||||||
time = { version = "0.2.7", default-features = false, features = ["std"] }
|
time = { version = "0.2.7", default-features = false, features = ["std"] }
|
||||||
|
thiserror = "1.0.11"
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
open-ssl = { version="0.10", package = "openssl", optional = true }
|
open-ssl = { version="0.10", package = "openssl", optional = true }
|
||||||
rust-tls = { version = "0.17.0", package = "rustls", optional = true }
|
rust-tls = { version = "0.17.0", package = "rustls", optional = true }
|
||||||
|
|
72
src/error.rs
72
src/error.rs
|
@ -1,54 +1,50 @@
|
||||||
//! Error and Result module
|
//! Error and Result module
|
||||||
pub use actix_http::error::*;
|
pub use actix_http::error::*;
|
||||||
use derive_more::{Display, From};
|
|
||||||
use serde_json::error::Error as JsonError;
|
use serde_json::error::Error as JsonError;
|
||||||
|
use thiserror::Error;
|
||||||
use url::ParseError as UrlParseError;
|
use url::ParseError as UrlParseError;
|
||||||
|
|
||||||
use crate::http::StatusCode;
|
use crate::http::StatusCode;
|
||||||
use crate::HttpResponse;
|
use crate::HttpResponse;
|
||||||
|
|
||||||
/// Errors which can occur when attempting to generate resource uri.
|
/// Errors which can occur when attempting to generate resource uri.
|
||||||
#[derive(Debug, PartialEq, Display, From)]
|
#[derive(Debug, PartialEq, Error)]
|
||||||
pub enum UrlGenerationError {
|
pub enum UrlGenerationError {
|
||||||
/// Resource not found
|
/// Resource not found
|
||||||
#[display(fmt = "Resource not found")]
|
#[error("Resource not found")]
|
||||||
ResourceNotFound,
|
ResourceNotFound,
|
||||||
/// Not all path pattern covered
|
/// Not all path pattern covered
|
||||||
#[display(fmt = "Not all path pattern covered")]
|
#[error("Not all path pattern covered")]
|
||||||
NotEnoughElements,
|
NotEnoughElements,
|
||||||
/// URL parse error
|
/// URL parse error
|
||||||
#[display(fmt = "{}", _0)]
|
#[error(transparent)]
|
||||||
ParseError(UrlParseError),
|
ParseError(#[from] UrlParseError),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `InternalServerError` for `UrlGeneratorError`
|
/// `InternalServerError` for `UrlGeneratorError`
|
||||||
impl ResponseError for UrlGenerationError {}
|
impl ResponseError for UrlGenerationError {}
|
||||||
|
|
||||||
/// A set of errors that can occur during parsing urlencoded payloads
|
/// A set of errors that can occur during parsing urlencoded payloads
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Error)]
|
||||||
pub enum UrlencodedError {
|
pub enum UrlencodedError {
|
||||||
/// Can not decode chunked transfer encoding
|
/// Can not decode chunked transfer encoding
|
||||||
#[display(fmt = "Can not decode chunked transfer encoding")]
|
#[error("Can not decode chunked transfer encoding")]
|
||||||
Chunked,
|
Chunked,
|
||||||
/// Payload size is bigger than allowed. (default: 256kB)
|
/// Payload size is bigger than allowed. (default: 256kB)
|
||||||
#[display(
|
#[error("Urlencoded payload size is bigger ({size} bytes) than allowed (default: {limit} bytes)")]
|
||||||
fmt = "Urlencoded payload size is bigger ({} bytes) than allowed (default: {} bytes)",
|
|
||||||
size,
|
|
||||||
limit
|
|
||||||
)]
|
|
||||||
Overflow { size: usize, limit: usize },
|
Overflow { size: usize, limit: usize },
|
||||||
/// Payload size is now known
|
/// Payload size is now known
|
||||||
#[display(fmt = "Payload size is now known")]
|
#[error("Payload size is now known")]
|
||||||
UnknownLength,
|
UnknownLength,
|
||||||
/// Content type error
|
/// Content type error
|
||||||
#[display(fmt = "Content type error")]
|
#[error("Content type error")]
|
||||||
ContentType,
|
ContentType,
|
||||||
/// Parse error
|
/// Parse error
|
||||||
#[display(fmt = "Parse error")]
|
#[error("Parse error")]
|
||||||
Parse,
|
Parse,
|
||||||
/// Payload error
|
/// Payload error
|
||||||
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
#[error("Error that occur during reading payload: {0}")]
|
||||||
Payload(PayloadError),
|
Payload(#[from] PayloadError),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `UrlencodedError`
|
/// Return `BadRequest` for `UrlencodedError`
|
||||||
|
@ -63,20 +59,20 @@ impl ResponseError for UrlencodedError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A set of errors that can occur during parsing json payloads
|
/// A set of errors that can occur during parsing json payloads
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Error)]
|
||||||
pub enum JsonPayloadError {
|
pub enum JsonPayloadError {
|
||||||
/// Payload size is bigger than allowed. (default: 32kB)
|
/// Payload size is bigger than allowed. (default: 32kB)
|
||||||
#[display(fmt = "Json payload size is bigger than allowed")]
|
#[error("Json payload size is bigger than allowed")]
|
||||||
Overflow,
|
Overflow,
|
||||||
/// Content type error
|
/// Content type error
|
||||||
#[display(fmt = "Content type error")]
|
#[error("Content type error")]
|
||||||
ContentType,
|
ContentType,
|
||||||
/// Deserialize error
|
/// Deserialize error
|
||||||
#[display(fmt = "Json deserialize error: {}", _0)]
|
#[error("Json deserialize error: {0}")]
|
||||||
Deserialize(JsonError),
|
Deserialize(#[from] JsonError),
|
||||||
/// Payload error
|
/// Payload error
|
||||||
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
#[error("Error that occur during reading payload: {0}")]
|
||||||
Payload(PayloadError),
|
Payload(#[from] PayloadError),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `JsonPayloadError`
|
/// Return `BadRequest` for `JsonPayloadError`
|
||||||
|
@ -92,11 +88,11 @@ impl ResponseError for JsonPayloadError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A set of errors that can occur during parsing request paths
|
/// A set of errors that can occur during parsing request paths
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Error)]
|
||||||
pub enum PathError {
|
pub enum PathError {
|
||||||
/// Deserialize error
|
/// Deserialize error
|
||||||
#[display(fmt = "Path deserialize error: {}", _0)]
|
#[error("Path deserialize error: {0}")]
|
||||||
Deserialize(serde::de::value::Error),
|
Deserialize(#[from] serde::de::value::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `PathError`
|
/// Return `BadRequest` for `PathError`
|
||||||
|
@ -107,11 +103,11 @@ impl ResponseError for PathError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A set of errors that can occur during parsing query strings
|
/// A set of errors that can occur during parsing query strings
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Error)]
|
||||||
pub enum QueryPayloadError {
|
pub enum QueryPayloadError {
|
||||||
/// Deserialize error
|
/// Deserialize error
|
||||||
#[display(fmt = "Query deserialize error: {}", _0)]
|
#[error("Query deserialize error: {0}")]
|
||||||
Deserialize(serde::de::value::Error),
|
Deserialize(#[from] serde::de::value::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `QueryPayloadError`
|
/// Return `BadRequest` for `QueryPayloadError`
|
||||||
|
@ -122,21 +118,21 @@ impl ResponseError for QueryPayloadError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Error type returned when reading body as lines.
|
/// Error type returned when reading body as lines.
|
||||||
#[derive(From, Display, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum ReadlinesError {
|
pub enum ReadlinesError {
|
||||||
/// Error when decoding a line.
|
/// Error when decoding a line.
|
||||||
#[display(fmt = "Encoding error")]
|
#[error("Encoding error")]
|
||||||
/// Payload size is bigger than allowed. (default: 256kB)
|
/// Payload size is bigger than allowed. (default: 256kB)
|
||||||
EncodingError,
|
EncodingError,
|
||||||
/// Payload error.
|
/// Payload error.
|
||||||
#[display(fmt = "Error that occur during reading payload: {}", _0)]
|
#[error("Error that occur during reading payload: {0}")]
|
||||||
Payload(PayloadError),
|
Payload(#[from] PayloadError),
|
||||||
/// Line limit exceeded.
|
/// Line limit exceeded.
|
||||||
#[display(fmt = "Line limit exceeded")]
|
#[error("Line limit exceeded")]
|
||||||
LimitOverflow,
|
LimitOverflow,
|
||||||
/// ContentType error.
|
/// ContentType error.
|
||||||
#[display(fmt = "Content-type error")]
|
#[error("Content-type error")]
|
||||||
ContentTypeError(ContentTypeError),
|
ContentTypeError(#[from] ContentTypeError),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `ReadlinesError`
|
/// Return `BadRequest` for `ReadlinesError`
|
||||||
|
|
|
@ -250,15 +250,15 @@ impl Default for PathConfig {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use actix_router::ResourceDef;
|
use actix_router::ResourceDef;
|
||||||
use derive_more::Display;
|
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::test::TestRequest;
|
use crate::test::TestRequest;
|
||||||
use crate::{error, http, HttpResponse};
|
use crate::{error, http, HttpResponse};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Display)]
|
#[derive(Deserialize, Debug, Error)]
|
||||||
#[display(fmt = "MyStruct({}, {})", key, value)]
|
#[error("MyStruct({key}, {value})")]
|
||||||
struct MyStruct {
|
struct MyStruct {
|
||||||
key: String,
|
key: String,
|
||||||
value: String,
|
value: String,
|
||||||
|
|
|
@ -224,15 +224,16 @@ impl Default for QueryConfig {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use actix_http::http::StatusCode;
|
use actix_http::http::StatusCode;
|
||||||
use derive_more::Display;
|
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::error::InternalError;
|
use crate::error::InternalError;
|
||||||
use crate::test::TestRequest;
|
use crate::test::TestRequest;
|
||||||
use crate::HttpResponse;
|
use crate::HttpResponse;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Display)]
|
#[derive(Deserialize, Debug, Error)]
|
||||||
|
#[error("{id}")]
|
||||||
struct Id {
|
struct Id {
|
||||||
id: String,
|
id: String,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue