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

@ -3,7 +3,7 @@ use log::*;
use std::io::{Cursor, Read, Write};
use crate::{
error::{CapacityErrorType, Error, ProtocolErrorType, Result},
error::{CapacityError, Error, ProtocolErrorType, Result},
util::NonBlockingResult,
};
use input_buffer::{InputBuffer, MIN_READ};
@ -46,7 +46,7 @@ impl<Stream: Read + Write> HandshakeMachine<Stream> {
let read = buf
.prepare_reserve(MIN_READ)
.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)
.no_block()?;
match read {

@ -8,7 +8,7 @@ mod mask;
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 log::*;
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`
// is not too big (fits into `usize`).
if length > max_size as u64 {
return Err(Error::Capacity(CapacityErrorType::MessageTooLong {
return Err(Error::Capacity(CapacityError::MessageTooLong {
size: length as usize,
max_size,
}));
@ -156,7 +156,7 @@ impl FrameCodec {
.in_buffer
.prepare_reserve(MIN_READ)
.with_limit(usize::max_value())
.map_err(|_| Error::Capacity(CapacityErrorType::TcpBufferFull))?
.map_err(|_| Error::Capacity(CapacityError::TcpBufferFull))?
.read_from(stream)?;
if size == 0 {
trace!("no frame received");
@ -207,7 +207,7 @@ impl FrameCodec {
#[cfg(test)]
mod tests {
use crate::error::{CapacityErrorType, Error};
use crate::error::{CapacityError, Error};
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 mut sock = FrameSocket::new(raw);
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!(),
}
}

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

@ -669,7 +669,7 @@ impl<T> CheckConnectionReset for Result<T> {
#[cfg(test)]
mod tests {
use super::{Message, Role, WebSocket, WebSocketConfig};
use crate::error::{CapacityErrorType, Error};
use crate::error::{CapacityError, Error};
use std::{io, io::Cursor};
@ -714,7 +714,7 @@ mod tests {
let mut socket = WebSocket::from_raw_socket(WriteMoc(incoming), Role::Client, Some(limit));
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!(),
}
}
@ -726,7 +726,7 @@ mod tests {
let mut socket = WebSocket::from_raw_socket(WriteMoc(incoming), Role::Client, Some(limit));
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!(),
}
}

Loading…
Cancel
Save