Skip to content

Commit 46ee883

Browse files
faerntomusdrw
authored andcommitted
Implement the Error trait on ws Error type (#117)
* Implement the Error trait on ws Error type * Fix compile error in test
1 parent 9128bb8 commit 46ee883

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

ws/src/server_builder.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::io;
23
use std::net::SocketAddr;
34
use std::sync::Arc;
@@ -37,6 +38,28 @@ impl From<io::Error> for Error {
3738
}
3839
}
3940

41+
impl fmt::Display for Error {
42+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43+
match *self {
44+
Error::Io(ref e) => e.fmt(f),
45+
Error::WebSocket(ref e) => e.fmt(f),
46+
}
47+
}
48+
}
49+
50+
impl ::std::error::Error for Error {
51+
fn description(&self) -> &str {
52+
"Starting the JSON-RPC WebSocket server failed"
53+
}
54+
55+
fn cause(&self) -> Option<&::std::error::Error> {
56+
Some(match *self {
57+
Error::Io(ref e) => e,
58+
Error::WebSocket(ref e) => e,
59+
})
60+
}
61+
}
62+
4063
/// Builder for `WebSockets` server
4164
pub struct ServerBuilder<M: core::Metadata, S: core::Middleware<M>> {
4265
handler: Arc<core::MetaIoHandler<M, S>>,

ws/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn serve(port: u16) -> Server {
7070
.request_middleware(|req: &ws::Request| {
7171
if req.resource() == "/intercepted" {
7272
let mut res = ws::Response::new(200, "OK");
73-
res.set_body(b"Hello World!");
73+
res.set_body("Hello World!".to_owned());
7474
Some(res)
7575
} else {
7676
None

0 commit comments

Comments
 (0)