Skip to content

Commit

Permalink
QPACK: use err_derive for public errors
Browse files Browse the repository at this point in the history
  • Loading branch information
stammw authored and djc committed Feb 8, 2019
1 parent 2fec389 commit dafe685
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
5 changes: 3 additions & 2 deletions quinn-h3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ maintenance = { status = "experimental" }
travis-ci = { repository = "djc/quinn" }

[dependencies]
quinn-proto = { path = "../quinn-proto", version = "0.2.0" }
quinn = { path = "../quinn", version = "0.2.0" }
bytes = "0.4.7"
bitlab = "0.8.1"
lazy_static = "1"
quinn-proto = { path = "../quinn-proto", version = "0.2.0" }
quinn = { path = "../quinn", version = "0.2.0" }
err-derive = "0.1.5"

[dev-dependencies]
proptest = "0.6.0"
Expand Down
13 changes: 12 additions & 1 deletion quinn-h3/src/qpack/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bytes::{Buf, BufMut};
use std::io::Cursor;

use err_derive::Error;

use super::static_::{Error as StaticError, StaticTable};
use super::vas;
use super::{
Expand All @@ -20,16 +22,25 @@ use super::parse_error::ParseError;
use super::prefix_int;
use super::prefix_string;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Error)]
pub enum Error {
#[error(display = "failed to parse integer: {:?}", _0)]
InvalidInteger(prefix_int::Error),
#[error(display = "failed to parse string: {:?}", _0)]
InvalidString(prefix_string::Error),
#[error(display = "index is out of dynamic table bounds: {:?}", _0)]
InvalidIndex(vas::Error),
#[error(display = "dynamic table error: {}", _0)]
DynamicTableError(DynamicTableError),
#[error(display = "index '{}' is out of static table bounds", _0)]
InvalidStaticIndex(usize),
#[error(display = "invalid data prefix")]
UnknownPrefix,
#[error(display = "missing references from dynamic table to decode header block")]
MissingRefs,
#[error(display = "header prefix contains invalid base index: {:?}", _0)]
BadBaseIndex(isize),
#[error(display = "data is unexpectedly truncated")]
UnexpectedEnd,
}

Expand Down
18 changes: 14 additions & 4 deletions quinn-h3/src/qpack/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::collections::btree_map::Entry as BTEntry;
use std::collections::hash_map::Entry;
use std::collections::{BTreeMap, HashMap, VecDeque};

use err_derive::Error;

use super::field::HeaderField;
use super::static_::StaticTable;
use crate::qpack::vas::{self, VirtualAddressSpace};
Expand All @@ -19,15 +21,23 @@ pub const SETTINGS_HEADER_TABLE_SIZE_DEFAULT: usize = 4096;
*/
const SETTINGS_HEADER_TABLE_SIZE_MAX: usize = 1073741823; // 2^30 -1

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Error)]
pub enum Error {
#[error(display = "bad relative index: {}", _0)]
BadRelativeIndex(usize),
#[error(display = "bad post base index: {}", _0)]
BadPostbaseIndex(usize),
#[error(display = "decoded index out of bounds: {}", _0)]
BadIndex(usize),
#[error(display = "tried to insert a field greater than dynamic table available size")]
MaxTableSizeReached,
#[error(display = "table size setting is greater than maximum authorized")]
MaximumTableSizeTooLarge,
#[error(display = "stream id '{}' is unknown or has already been acknowledged or canceled", _0)]
UnknownStreamId(u64),
#[error(display = "tried to acknowledge encoder stream but no encoder data has been sent")]
NoTrackingData,
#[error(display = "internal reference tracking error")]
InvalidTrackingCount,
}

Expand Down Expand Up @@ -858,9 +868,9 @@ mod tests {

insert_fields(&mut table, vec![HeaderField::new("Name-C", "Value-C")]);

assert_eq!(table.get(0), Ok(&HeaderField::new("Name-B", "Value-B")));
assert_eq!(table.get(1), Ok(&HeaderField::new("Name-C", "Value-C")));
assert_eq!(table.get(2), Err(Error::BadIndex(2)));
assert_eq!(table.fields.get(0), Some(&HeaderField::new("Name-B", "Value-B")));
assert_eq!(table.fields.get(1), Some(&HeaderField::new("Name-C", "Value-C")));
assert_eq!(table.fields.get(2), None);
}

#[test]
Expand Down
25 changes: 16 additions & 9 deletions quinn-h3/src/qpack/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use bytes::{Buf, BufMut};
use std::cmp;
use std::io::Cursor;

use bytes::{Buf, BufMut};

use err_derive::Error;

use super::block::{
HeaderPrefix, Indexed, IndexedWithPostBase, Literal, LiteralWithNameRef,
LiteralWithPostBaseNameRef,
Expand All @@ -20,6 +23,18 @@ use super::stream::{
};
use super::HeaderField;

#[derive(Debug, PartialEq, Error)]
pub enum Error {
#[error(display = "failed to insert in dynamic table: {}", _0)]
Insertion(DynamicTableError),
#[error(display = "prefixed string: {:?}", _0)]
InvalidString(StringError),
#[error(display = "prefixed integer: {:?}", _0)]
InvalidInteger(IntError),
#[error(display = "invalid data prefix")]
UnknownPrefix,
}

pub fn encode<W: BufMut>(
table: &mut DynamicTableEncoder,
block: &mut W,
Expand Down Expand Up @@ -173,14 +188,6 @@ pub fn set_dynamic_table_size<W: BufMut>(
Ok(())
}

#[derive(Debug, PartialEq)]
pub enum Error {
Insertion(DynamicTableError),
InvalidString(StringError),
InvalidInteger(IntError),
UnknownPrefix,
}

impl From<DynamicTableError> for Error {
fn from(e: DynamicTableError) -> Self {
Error::Insertion(e)
Expand Down
5 changes: 3 additions & 2 deletions quinn-h3/src/qpack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ pub use self::decoder::{
};
pub use self::dynamic::{
DynamicTable, DynamicTableDecoder, DynamicTableEncoder, DynamicTableInserter,
Error as DynamicTableError,
};
pub use self::encoder::{encode, on_decoder_recv, set_dynamic_table_size, Error as EncoderError};
pub use self::field::HeaderField;

use self::dynamic::Error as DynamicTableError;

mod block;
mod dynamic;
mod field;
mod parse_error;
mod static_;
mod stream;
mod vas;
mod parse_error;

mod decoder;
mod encoder;
Expand Down

0 comments on commit dafe685

Please sign in to comment.