Skip to content

Commit 8f3057d

Browse files
committed
Use error-chain for error handling
1 parent 3b48ad0 commit 8f3057d

File tree

6 files changed

+37
-59
lines changed

6 files changed

+37
-59
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ default = []
1717
byteorder = "1.0"
1818
flate2 = { version = "0.2", features = ["zlib"], default-features = false }
1919
uuid = { version = "0.5", optional = true }
20+
error-chain = "0.10"
2021

src/error.rs

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/errors.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std;
2+
3+
error_chain! {
4+
types {
5+
Error, ErrorKind, ResultExt;
6+
}
7+
8+
foreign_links {
9+
Io(std::io::Error);
10+
FromUtf8(std::string::FromUtf8Error);
11+
TryFromIntError(std::num::TryFromIntError);
12+
CharTryFromError(std::char::CharTryFromError);
13+
14+
UuidParseError(::uuid::ParseError) #[cfg(feature = "uuid")];
15+
}
16+
17+
errors {
18+
UnknownPacketId {
19+
description("unknown packet identifier")
20+
display("unknown packet identifier")
21+
}
22+
}
23+
}
24+

src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@
77
88
pub use self::primitives::{Integer, DynArray, String};
99
pub use self::parcel::Parcel;
10-
pub use self::error::Error;
10+
pub use self::errors::{Error, ErrorKind, ResultExt};
1111

12+
// Must go first because it defines common macros.
1213
#[macro_use]
13-
pub mod primitives;
14+
mod packet;
1415

15-
mod parcel;
1616
#[macro_use]
17-
mod packet;
18-
mod error;
17+
pub mod primitives;
1918
#[macro_use]
2019
pub mod wire;
2120

21+
mod errors;
22+
mod parcel;
23+
2224
extern crate byteorder;
2325
extern crate flate2;
26+
#[macro_use]
27+
extern crate error_chain;
2428

2529
#[cfg(feature = "uuid")]
2630
extern crate uuid;

src/packet/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ macro_rules! define_packet_kind
8080

8181
let packet = match packet_id {
8282
$( $packet_id => $ty::$packet_ty(<$packet_ty as $crate::Parcel>::read(read)?), )+
83-
_ => return Err($crate::Error::UnknownPacketId),
83+
_ => return Err($crate::ErrorKind::UnknownPacketId.into()),
8484
};
8585

8686
Ok(packet)

src/packet/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[macro_use]
2-
pub mod macros;
2+
mod macros;
33
#[cfg(test)]
44
#[allow(unused_variables)]
5-
pub mod test;
5+
mod test;
66

0 commit comments

Comments
 (0)