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:
		
							
								
								
									
										34
									
								
								src/proto/error.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/proto/error.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| use frame::Reason; | ||||
| use codec::RecvError; | ||||
|  | ||||
| use std::io; | ||||
|  | ||||
| /// Either an H2 reason  or an I/O error | ||||
| #[derive(Debug)] | ||||
| pub enum Error { | ||||
|     Proto(Reason), | ||||
|     Io(io::Error), | ||||
| } | ||||
|  | ||||
| impl Error { | ||||
|     pub fn into_connection_recv_error(self) -> RecvError { | ||||
|         use self::Error::*; | ||||
|  | ||||
|         match self { | ||||
|             Proto(reason) => RecvError::Connection(reason), | ||||
|             Io(e) => RecvError::Io(e), | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl From<Reason> for Error { | ||||
|     fn from(src: Reason) -> Self { | ||||
|         Error::Proto(src) | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl From<io::Error> for Error { | ||||
|     fn from(src: io::Error) -> Self { | ||||
|         Error::Io(src) | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user