Refactor errors (#46)

This patch does a bunch of refactoring, mostly around error types, but it also
paves the way to allow `Codec` to be used standalone.

* `Codec` (and `FramedRead` / `FramedWrite`) is broken out into a codec module.
* An h2-codec crate is created that re-exports the frame and codec modules.
* New error types are introduced in the internals:
  * `RecvError` represents errors caused by trying to receive a frame.
  * `SendError` represents errors caused by trying to send a frame.
  * `UserError` is an enum of potential errors caused by invalid usage
    by the user of the lib.
  * `ProtoError` is either a `Reason` or an `io::Error`. However it doesn't
    specify connection or stream level.
  * `h2::Error` is an opaque error type and is the only error type exposed
    by the public API (used to be `ConnectionError`).

There are misc code changes to enable this as well. The biggest is a new "sink"
API for `Codec`. It provides buffer which queues up a frame followed by flush
which writes everything that is queued. This departs from the `Sink` trait in
order to provide more accurate error values. For example, buffer can never fail
(but it will panic if `poll_ready` is not called first).
This commit is contained in:
Carl Lerche
2017-09-02 11:12:50 -07:00
committed by GitHub
parent 6fd9674759
commit c122e97127
37 changed files with 1043 additions and 1027 deletions

View File

@@ -53,10 +53,10 @@ fn recv_invalid_server_stream_id() {
let stream = h2.request(request, true).unwrap();
// The connection errors
assert_proto_err!(h2.wait().unwrap_err(), ProtocolError);
assert!(h2.wait().is_err());
// The stream errors
assert_proto_err!(stream.wait().unwrap_err(), ProtocolError);
assert!(stream.wait().is_err());
}
#[test]

View File

@@ -89,27 +89,3 @@ pub mod frames {
pub const SETTINGS: &'static [u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0];
pub const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0];
}
#[macro_export]
macro_rules! assert_user_err {
($actual:expr, $err:ident) => {{
use h2::error::{ConnectionError, User};
match $actual {
ConnectionError::User(e) => assert_eq!(e, User::$err),
_ => panic!("unexpected connection error type"),
}
}};
}
#[macro_export]
macro_rules! assert_proto_err {
($actual:expr, $err:ident) => {{
use h2::error::{ConnectionError, Reason};
match $actual {
ConnectionError::Proto(e) => assert_eq!(e, Reason::$err),
_ => panic!("unexpected connection error type"),
}
}};
}