1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

standardize error messages in actix-http

This commit is contained in:
Rob Ede 2023-03-13 17:17:02 +00:00
parent 442fa279da
commit 5e29726c4f
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 52 additions and 54 deletions

View File

@ -161,44 +161,44 @@ impl From<crate::ws::ProtocolError> for Error {
#[non_exhaustive] #[non_exhaustive]
pub enum ParseError { pub enum ParseError {
/// An invalid `Method`, such as `GE.T`. /// An invalid `Method`, such as `GE.T`.
#[display(fmt = "Invalid Method specified")] #[display(fmt = "invalid method specified")]
Method, Method,
/// An invalid `Uri`, such as `exam ple.domain`. /// An invalid `Uri`, such as `exam ple.domain`.
#[display(fmt = "Uri error: {}", _0)] #[display(fmt = "URI error: {}", _0)]
Uri(InvalidUri), Uri(InvalidUri),
/// An invalid `HttpVersion`, such as `HTP/1.1` /// An invalid `HttpVersion`, such as `HTP/1.1`
#[display(fmt = "Invalid HTTP version specified")] #[display(fmt = "invalid HTTP version specified")]
Version, Version,
/// An invalid `Header`. /// An invalid `Header`.
#[display(fmt = "Invalid Header provided")] #[display(fmt = "invalid Header provided")]
Header, Header,
/// A message head is too large to be reasonable. /// A message head is too large to be reasonable.
#[display(fmt = "Message head is too large")] #[display(fmt = "message head is too large")]
TooLarge, TooLarge,
/// A message reached EOF, but is not complete. /// A message reached EOF, but is not complete.
#[display(fmt = "Message is incomplete")] #[display(fmt = "message is incomplete")]
Incomplete, Incomplete,
/// An invalid `Status`, such as `1337 ELITE`. /// An invalid `Status`, such as `1337 ELITE`.
#[display(fmt = "Invalid Status provided")] #[display(fmt = "invalid status provided")]
Status, Status,
/// A timeout occurred waiting for an IO event. /// A timeout occurred waiting for an IO event.
#[allow(dead_code)] #[allow(dead_code)]
#[display(fmt = "Timeout")] #[display(fmt = "timeout")]
Timeout, Timeout,
/// An `io::Error` that occurred while trying to read or write to a network stream. /// An I/O error that occurred while trying to read or write to a network stream.
#[display(fmt = "IO error: {}", _0)] #[display(fmt = "I/O error: {}", _0)]
Io(io::Error), Io(io::Error),
/// Parsing a field as string failed. /// Parsing a field as string failed.
#[display(fmt = "UTF8 error: {}", _0)] #[display(fmt = "UTF-8 error: {}", _0)]
Utf8(Utf8Error), Utf8(Utf8Error),
} }
@ -257,22 +257,19 @@ impl From<ParseError> for Response<BoxBody> {
#[non_exhaustive] #[non_exhaustive]
pub enum PayloadError { pub enum PayloadError {
/// A payload reached EOF, but is not complete. /// A payload reached EOF, but is not complete.
#[display( #[display(fmt = "payload reached EOF before completing: {:?}", _0)]
fmt = "A payload reached EOF, but is not complete. Inner error: {:?}",
_0
)]
Incomplete(Option<io::Error>), Incomplete(Option<io::Error>),
/// Content encoding stream corruption. /// Content encoding stream corruption.
#[display(fmt = "Can not decode content-encoding.")] #[display(fmt = "can not decode content-encoding")]
EncodingCorrupted, EncodingCorrupted,
/// Payload reached size limit. /// Payload reached size limit.
#[display(fmt = "Payload reached size limit.")] #[display(fmt = "payload reached size limit")]
Overflow, Overflow,
/// Payload length is unknown. /// Payload length is unknown.
#[display(fmt = "Payload length is unknown.")] #[display(fmt = "payload length is unknown")]
UnknownLength, UnknownLength,
/// HTTP/2 payload error. /// HTTP/2 payload error.
@ -330,22 +327,23 @@ impl From<PayloadError> for Error {
#[non_exhaustive] #[non_exhaustive]
pub enum DispatchError { pub enum DispatchError {
/// Service error. /// Service error.
#[display(fmt = "Service Error")] #[display(fmt = "service error")]
Service(Response<BoxBody>), Service(Response<BoxBody>),
/// Body streaming error. /// Body streaming error.
#[display(fmt = "Body error: {}", _0)] #[display(fmt = "body error: {}", _0)]
Body(Box<dyn StdError>), Body(Box<dyn StdError>),
/// Upgrade service error. /// Upgrade service error.
#[display(fmt = "upgrade error")]
Upgrade, Upgrade,
/// An `io::Error` that occurred while trying to read or write to a network stream. /// An `io::Error` that occurred while trying to read or write to a network stream.
#[display(fmt = "IO error: {}", _0)] #[display(fmt = "I/O error: {}", _0)]
Io(io::Error), Io(io::Error),
/// Request parse error. /// Request parse error.
#[display(fmt = "Request parse error: {}", _0)] #[display(fmt = "request parse error: {}", _0)]
Parse(ParseError), Parse(ParseError),
/// HTTP/2 error. /// HTTP/2 error.
@ -354,19 +352,19 @@ pub enum DispatchError {
H2(h2::Error), H2(h2::Error),
/// The first request did not complete within the specified timeout. /// The first request did not complete within the specified timeout.
#[display(fmt = "The first request did not complete within the specified timeout")] #[display(fmt = "request did not complete within the specified timeout")]
SlowRequestTimeout, SlowRequestTimeout,
/// Disconnect timeout. Makes sense for ssl streams. /// Disconnect timeout. Makes sense for TLS streams.
#[display(fmt = "Connection shutdown timeout")] #[display(fmt = "connection shutdown timeout")]
DisconnectTimeout, DisconnectTimeout,
/// Handler dropped payload before reading EOF. /// Handler dropped payload before reading EOF.
#[display(fmt = "Handler dropped payload before reading EOF")] #[display(fmt = "handler dropped payload before reading EOF")]
HandlerDroppedPayload, HandlerDroppedPayload,
/// Internal error. /// Internal error.
#[display(fmt = "Internal error")] #[display(fmt = "internal error")]
InternalError, InternalError,
} }
@ -391,12 +389,12 @@ impl StdError for DispatchError {
#[cfg_attr(test, derive(PartialEq, Eq))] #[cfg_attr(test, derive(PartialEq, Eq))]
#[non_exhaustive] #[non_exhaustive]
pub enum ContentTypeError { pub enum ContentTypeError {
/// Can not parse content type /// Can not parse content type.
#[display(fmt = "Can not parse content type")] #[display(fmt = "could not parse content type")]
ParseError, ParseError,
/// Unknown content encoding /// Unknown content encoding.
#[display(fmt = "Unknown content encoding")] #[display(fmt = "unknown content encoding")]
UnknownEncoding, UnknownEncoding,
} }
@ -424,7 +422,7 @@ mod tests {
let err: Error = ParseError::Io(orig).into(); let err: Error = ParseError::Io(orig).into();
assert_eq!( assert_eq!(
format!("{}", err), format!("{}", err),
"error parsing HTTP message: IO error: other" "error parsing HTTP message: I/O error: other"
); );
} }
@ -451,7 +449,7 @@ mod tests {
let err = PayloadError::Incomplete(None); let err = PayloadError::Incomplete(None);
assert_eq!( assert_eq!(
err.to_string(), err.to_string(),
"A payload reached EOF, but is not complete. Inner error: None" "payload reached EOF before completing: None"
); );
} }
@ -471,7 +469,7 @@ mod tests {
match ParseError::from($from) { match ParseError::from($from) {
e @ $error => { e @ $error => {
let desc = format!("{}", e); let desc = format!("{}", e);
assert_eq!(desc, format!("IO error: {}", $from)); assert_eq!(desc, format!("I/O error: {}", $from));
} }
_ => unreachable!("{:?}", $from), _ => unreachable!("{:?}", $from),
} }

View File

@ -26,39 +26,39 @@ pub use self::proto::{hash_key, CloseCode, CloseReason, OpCode};
#[derive(Debug, Display, Error, From)] #[derive(Debug, Display, Error, From)]
pub enum ProtocolError { pub enum ProtocolError {
/// Received an unmasked frame from client. /// Received an unmasked frame from client.
#[display(fmt = "Received an unmasked frame from client.")] #[display(fmt = "received an unmasked frame from client")]
UnmaskedFrame, UnmaskedFrame,
/// Received a masked frame from server. /// Received a masked frame from server.
#[display(fmt = "Received a masked frame from server.")] #[display(fmt = "received a masked frame from server")]
MaskedFrame, MaskedFrame,
/// Encountered invalid opcode. /// Encountered invalid opcode.
#[display(fmt = "Invalid opcode: {}.", _0)] #[display(fmt = "invalid opcode ({})", _0)]
InvalidOpcode(#[error(not(source))] u8), InvalidOpcode(#[error(not(source))] u8),
/// Invalid control frame length /// Invalid control frame length
#[display(fmt = "Invalid control frame length: {}.", _0)] #[display(fmt = "invalid control frame length ({})", _0)]
InvalidLength(#[error(not(source))] usize), InvalidLength(#[error(not(source))] usize),
/// Bad opcode. /// Bad opcode.
#[display(fmt = "Bad opcode.")] #[display(fmt = "bad opcode")]
BadOpCode, BadOpCode,
/// A payload reached size limit. /// A payload reached size limit.
#[display(fmt = "A payload reached size limit.")] #[display(fmt = "payload reached size limit")]
Overflow, Overflow,
/// Continuation is not started. /// Continuation has not started.
#[display(fmt = "Continuation is not started.")] #[display(fmt = "continuation has not started")]
ContinuationNotStarted, ContinuationNotStarted,
/// Received new continuation but it is already started. /// Received new continuation but it is already started.
#[display(fmt = "Received new continuation but it is already started.")] #[display(fmt = "received new continuation but it has already started")]
ContinuationStarted, ContinuationStarted,
/// Unknown continuation fragment. /// Unknown continuation fragment.
#[display(fmt = "Unknown continuation fragment: {}.", _0)] #[display(fmt = "unknown continuation fragment: {}", _0)]
ContinuationFragment(#[error(not(source))] OpCode), ContinuationFragment(#[error(not(source))] OpCode),
/// I/O error. /// I/O error.
@ -70,27 +70,27 @@ pub enum ProtocolError {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, Error)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Display, Error)]
pub enum HandshakeError { pub enum HandshakeError {
/// Only get method is allowed. /// Only get method is allowed.
#[display(fmt = "Method not allowed.")] #[display(fmt = "method not allowed")]
GetMethodRequired, GetMethodRequired,
/// Upgrade header if not set to WebSocket. /// Upgrade header if not set to WebSocket.
#[display(fmt = "WebSocket upgrade is expected.")] #[display(fmt = "WebSocket upgrade is expected")]
NoWebsocketUpgrade, NoWebsocketUpgrade,
/// Connection header is not set to upgrade. /// Connection header is not set to upgrade.
#[display(fmt = "Connection upgrade is expected.")] #[display(fmt = "connection upgrade is expected")]
NoConnectionUpgrade, NoConnectionUpgrade,
/// WebSocket version header is not set. /// WebSocket version header is not set.
#[display(fmt = "WebSocket version header is required.")] #[display(fmt = "WebSocket version header is required")]
NoVersionHeader, NoVersionHeader,
/// Unsupported WebSocket version. /// Unsupported WebSocket version.
#[display(fmt = "Unsupported WebSocket version.")] #[display(fmt = "unsupported WebSocket version")]
UnsupportedVersion, UnsupportedVersion,
/// WebSocket key is not set or wrong. /// WebSocket key is not set or wrong.
#[display(fmt = "Unknown websocket key.")] #[display(fmt = "unknown WebSocket key")]
BadWebsocketKey, BadWebsocketKey,
} }

View File

@ -39,13 +39,13 @@ impl WsService {
#[derive(Debug, Display, Error, From)] #[derive(Debug, Display, Error, From)]
enum WsServiceError { enum WsServiceError {
#[display(fmt = "http error")] #[display(fmt = "HTTP error")]
Http(actix_http::Error), Http(actix_http::Error),
#[display(fmt = "ws handshake error")] #[display(fmt = "WS handshake error")]
Ws(actix_http::ws::HandshakeError), Ws(actix_http::ws::HandshakeError),
#[display(fmt = "io error")] #[display(fmt = "I/O error")]
Io(std::io::Error), Io(std::io::Error),
#[display(fmt = "dispatcher error")] #[display(fmt = "dispatcher error")]

View File

@ -152,7 +152,7 @@ mod tests {
let resp_err: &dyn ResponseError = &err; let resp_err: &dyn ResponseError = &err;
let err = resp_err.downcast_ref::<PayloadError>().unwrap(); let err = resp_err.downcast_ref::<PayloadError>().unwrap();
assert_eq!(err.to_string(), "Payload reached size limit."); assert_eq!(err.to_string(), "payload reached size limit");
let not_err = resp_err.downcast_ref::<ContentTypeError>(); let not_err = resp_err.downcast_ref::<ContentTypeError>();
assert!(not_err.is_none()); assert!(not_err.is_none());