Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try_ methods to handle capacity overflow #617

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum ErrorKind {
UriParts(uri::InvalidUriParts),
HeaderName(header::InvalidHeaderName),
HeaderValue(header::InvalidHeaderValue),
MaxSizeReached(MaxSizeReached),
}

impl fmt::Debug for Error {
Expand Down Expand Up @@ -61,6 +62,7 @@ impl Error {
UriParts(ref e) => e,
HeaderName(ref e) => e,
HeaderValue(ref e) => e,
MaxSizeReached(ref e) => e,
}
}
}
Expand All @@ -73,6 +75,14 @@ impl error::Error for Error {
}
}

impl From<MaxSizeReached> for Error {
fn from(err: MaxSizeReached) -> Error {
Error {
inner: ErrorKind::MaxSizeReached(err),
}
}
}

impl From<status::InvalidStatusCode> for Error {
fn from(err: status::InvalidStatusCode) -> Error {
Error {
Expand Down Expand Up @@ -127,6 +137,35 @@ impl From<std::convert::Infallible> for Error {
}
}

/// Error returned when max capacity of `HeaderMap` is exceeded
pub struct MaxSizeReached {
seanmonstar marked this conversation as resolved.
Show resolved Hide resolved
_priv: (),
}

impl MaxSizeReached {
/// Create new `MaxSizeReached` instance
pub fn new() -> MaxSizeReached {
MaxSizeReached { _priv: () }
}
}

impl fmt::Debug for MaxSizeReached {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MaxSizeReached")
// skip _priv noise
.finish()
}
}

impl fmt::Display for MaxSizeReached {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("max size reached")
}
}

impl std::error::Error for MaxSizeReached {}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading