Rename CapacityErrorType to just CapacityError, implement using thiserror

pull/168/head
WiredSound 4 years ago
parent e6d66698a3
commit 652a6b776e
  1. 25
      src/error.rs
  2. 4
      src/handshake/machine.rs
  3. 10
      src/protocol/frame/mod.rs
  4. 4
      src/protocol/message.rs
  5. 6
      src/protocol/mod.rs

@ -52,7 +52,7 @@ pub enum Error {
/// - When writing: your message is bigger than the configured max message size /// - When writing: your message is bigger than the configured max message size
/// (64MB by default). /// (64MB by default).
#[error("Space limit exceeded: {0}")] #[error("Space limit exceeded: {0}")]
Capacity(CapacityErrorType), Capacity(CapacityError),
/// Protocol violation. /// Protocol violation.
#[error("WebSocket protocol error: {0}")] #[error("WebSocket protocol error: {0}")]
Protocol(ProtocolErrorType), Protocol(ProtocolErrorType),
@ -118,20 +118,23 @@ impl From<http::status::InvalidStatusCode> for Error {
impl From<httparse::Error> for Error { impl From<httparse::Error> for Error {
fn from(err: httparse::Error) -> Self { fn from(err: httparse::Error) -> Self {
match err { match err {
httparse::Error::TooManyHeaders => Error::Capacity(CapacityErrorType::TooManyHeaders), httparse::Error::TooManyHeaders => Error::Capacity(CapacityError::TooManyHeaders),
e => Error::Protocol(ProtocolErrorType::HttparseError(e)), e => Error::Protocol(ProtocolErrorType::HttparseError(e)),
} }
} }
} }
/// Indicates the specific type/cause of a capacity error. /// Indicates the specific type/cause of a capacity error.
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Error, Debug, PartialEq, Eq, Clone, Copy)]
pub enum CapacityErrorType { pub enum CapacityError {
/// Too many headers provided (see [`httparse::Error::TooManyHeaders`]). /// Too many headers provided (see [`httparse::Error::TooManyHeaders`]).
#[error("Too many headers")]
TooManyHeaders, TooManyHeaders,
/// Received header is too long. /// Received header is too long.
#[error("Header too long")]
HeaderTooLong, HeaderTooLong,
/// Message is bigger than the maximum allowed size. /// Message is bigger than the maximum allowed size.
#[error("Message too long: {size} > {max_size}")]
MessageTooLong { MessageTooLong {
/// The size of the message. /// The size of the message.
size: usize, size: usize,
@ -139,22 +142,10 @@ pub enum CapacityErrorType {
max_size: usize, max_size: usize,
}, },
/// TCP buffer is full. /// TCP buffer is full.
#[error("Incoming TCP buffer is full")]
TcpBufferFull, TcpBufferFull,
} }
impl fmt::Display for CapacityErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CapacityErrorType::TooManyHeaders => write!(f, "Too many headers"),
CapacityErrorType::HeaderTooLong => write!(f, "Header too long"),
CapacityErrorType::MessageTooLong { size, max_size } => {
write!(f, "Message too long: {} > {}", size, max_size)
}
CapacityErrorType::TcpBufferFull => write!(f, "Incoming TCP buffer is full"),
}
}
}
/// Indicates the specific type/cause of a protocol error. /// Indicates the specific type/cause of a protocol error.
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ProtocolErrorType { pub enum ProtocolErrorType {

@ -3,7 +3,7 @@ use log::*;
use std::io::{Cursor, Read, Write}; use std::io::{Cursor, Read, Write};
use crate::{ use crate::{
error::{CapacityErrorType, Error, ProtocolErrorType, Result}, error::{CapacityError, Error, ProtocolErrorType, Result},
util::NonBlockingResult, util::NonBlockingResult,
}; };
use input_buffer::{InputBuffer, MIN_READ}; use input_buffer::{InputBuffer, MIN_READ};
@ -46,7 +46,7 @@ impl<Stream: Read + Write> HandshakeMachine<Stream> {
let read = buf let read = buf
.prepare_reserve(MIN_READ) .prepare_reserve(MIN_READ)
.with_limit(usize::max_value()) // TODO limit size .with_limit(usize::max_value()) // TODO limit size
.map_err(|_| Error::Capacity(CapacityErrorType::HeaderTooLong))? .map_err(|_| Error::Capacity(CapacityError::HeaderTooLong))?
.read_from(&mut self.stream) .read_from(&mut self.stream)
.no_block()?; .no_block()?;
match read { match read {

@ -8,7 +8,7 @@ mod mask;
pub use self::frame::{CloseFrame, Frame, FrameHeader}; pub use self::frame::{CloseFrame, Frame, FrameHeader};
use crate::error::{CapacityErrorType, Error, Result}; use crate::error::{CapacityError, Error, Result};
use input_buffer::{InputBuffer, MIN_READ}; use input_buffer::{InputBuffer, MIN_READ};
use log::*; use log::*;
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read, Write}; use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read, Write};
@ -133,7 +133,7 @@ impl FrameCodec {
// Enforce frame size limit early and make sure `length` // Enforce frame size limit early and make sure `length`
// is not too big (fits into `usize`). // is not too big (fits into `usize`).
if length > max_size as u64 { if length > max_size as u64 {
return Err(Error::Capacity(CapacityErrorType::MessageTooLong { return Err(Error::Capacity(CapacityError::MessageTooLong {
size: length as usize, size: length as usize,
max_size, max_size,
})); }));
@ -156,7 +156,7 @@ impl FrameCodec {
.in_buffer .in_buffer
.prepare_reserve(MIN_READ) .prepare_reserve(MIN_READ)
.with_limit(usize::max_value()) .with_limit(usize::max_value())
.map_err(|_| Error::Capacity(CapacityErrorType::TcpBufferFull))? .map_err(|_| Error::Capacity(CapacityError::TcpBufferFull))?
.read_from(stream)?; .read_from(stream)?;
if size == 0 { if size == 0 {
trace!("no frame received"); trace!("no frame received");
@ -207,7 +207,7 @@ impl FrameCodec {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::error::{CapacityErrorType, Error}; use crate::error::{CapacityError, Error};
use super::{Frame, FrameSocket}; use super::{Frame, FrameSocket};
@ -270,7 +270,7 @@ mod tests {
let raw = Cursor::new(vec![0x82, 0x07, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]); let raw = Cursor::new(vec![0x82, 0x07, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]);
let mut sock = FrameSocket::new(raw); let mut sock = FrameSocket::new(raw);
match sock.read_frame(Some(5)) { match sock.read_frame(Some(5)) {
Err(Error::Capacity(CapacityErrorType::MessageTooLong { size: 7, max_size: 5 })) => {} Err(Error::Capacity(CapacityError::MessageTooLong { size: 7, max_size: 5 })) => {}
_ => panic!(), _ => panic!(),
} }
} }

@ -6,7 +6,7 @@ use std::{
}; };
use super::frame::CloseFrame; use super::frame::CloseFrame;
use crate::error::{CapacityErrorType, Error, Result}; use crate::error::{CapacityError, Error, Result};
mod string_collect { mod string_collect {
use utf8::DecodeError; use utf8::DecodeError;
@ -122,7 +122,7 @@ impl IncompleteMessage {
let portion_size = tail.as_ref().len(); let portion_size = tail.as_ref().len();
// Be careful about integer overflows here. // Be careful about integer overflows here.
if my_size > max_size || portion_size > max_size - my_size { if my_size > max_size || portion_size > max_size - my_size {
return Err(Error::Capacity(CapacityErrorType::MessageTooLong { return Err(Error::Capacity(CapacityError::MessageTooLong {
size: my_size + portion_size, size: my_size + portion_size,
max_size, max_size,
})); }));

@ -669,7 +669,7 @@ impl<T> CheckConnectionReset for Result<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Message, Role, WebSocket, WebSocketConfig}; use super::{Message, Role, WebSocket, WebSocketConfig};
use crate::error::{CapacityErrorType, Error}; use crate::error::{CapacityError, Error};
use std::{io, io::Cursor}; use std::{io, io::Cursor};
@ -714,7 +714,7 @@ mod tests {
let mut socket = WebSocket::from_raw_socket(WriteMoc(incoming), Role::Client, Some(limit)); let mut socket = WebSocket::from_raw_socket(WriteMoc(incoming), Role::Client, Some(limit));
match socket.read_message() { match socket.read_message() {
Err(Error::Capacity(CapacityErrorType::MessageTooLong { size: 13, max_size: 10 })) => {} Err(Error::Capacity(CapacityError::MessageTooLong { size: 13, max_size: 10 })) => {}
_ => panic!(), _ => panic!(),
} }
} }
@ -726,7 +726,7 @@ mod tests {
let mut socket = WebSocket::from_raw_socket(WriteMoc(incoming), Role::Client, Some(limit)); let mut socket = WebSocket::from_raw_socket(WriteMoc(incoming), Role::Client, Some(limit));
match socket.read_message() { match socket.read_message() {
Err(Error::Capacity(CapacityErrorType::MessageTooLong { size: 3, max_size: 2 })) => {} Err(Error::Capacity(CapacityError::MessageTooLong { size: 3, max_size: 2 })) => {}
_ => panic!(), _ => panic!(),
} }
} }

Loading…
Cancel
Save