{"action":"created","issue":{"url":"https://api.github.com/repos/housleyjk/ws-rs/issues/88","repository_url":"https://api.github.com/repos/housleyjk/ws-rs","labels_url":"https://api.github.com/repos/housleyjk/ws-rs/issues/88/labels{/name}","comments_url":"https://api.github.com/repos/housleyjk/ws-rs/issues/88/comments","events_url":"https://api.github.com/repos/housleyjk/ws-rs/issues/88/events","html_url":"https://github.com/housleyjk/ws-rs/issues/88","id":185879125,"number":88,"title":"Code example for the Error Kind::Custom","user":{"login":"mfaerevaag","id":1221244,"avatar_url":"https://avatars.githubusercontent.com/u/1221244?v=3","gravatar_id":"","url":"https://api.github.com/users/mfaerevaag","html_url":"https://github.com/mfaerevaag","followers_url":"https://api.github.com/users/mfaerevaag/followers","following_url":"https://api.github.com/users/mfaerevaag/following{/other_user}","gists_url":"https://api.github.com/users/mfaerevaag/gists{/gist_id}","starred_url":"https://api.github.com/users/mfaerevaag/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mfaerevaag/subscriptions","organizations_url":"https://api.github.com/users/mfaerevaag/orgs","repos_url":"https://api.github.com/users/mfaerevaag/repos","events_url":"https://api.github.com/users/mfaerevaag/events{/privacy}","received_events_url":"https://api.github.com/users/mfaerevaag/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-10-28T09:32:00Z","updated_at":"2016-10-29T18:19:38Z","closed_at":null,"body":"I came across the `Kind::Custom`, which could be awesome to use, but since I'm quite new to Rust I'm having a hard time figuring out how.\r\n\r\n```rust\r\npub enum Kind {\r\n    ...\r\n    /// A custom error kind for use by applications. This error kind involves extra overhead\r\n    /// because it will allocate the memory on the heap. The WebSocket ignores such errors by\r\n    /// default, simply passing them to the Connection Handler.\r\n    Custom(Box<StdError + Send + Sync>),\r\n}\r\n```\r\n\r\nIt it possible to formulate a simple and short example on how to do this?\r\n\r\nJust a suggestion. And thanks for beautifully implemented lib 🤘 "},"comment":{"url":"https://api.github.com/repos/housleyjk/ws-rs/issues/comments/257107281","html_url":"https://github.com/housleyjk/ws-rs/issues/88#issuecomment-257107281","issue_url":"https://api.github.com/repos/housleyjk/ws-rs/issues/88","id":257107281,"user":{"login":"housleyjk","id":5325564,"avatar_url":"https://avatars.githubusercontent.com/u/5325564?v=3","gravatar_id":"","url":"https://api.github.com/users/housleyjk","html_url":"https://github.com/housleyjk","followers_url":"https://api.github.com/users/housleyjk/followers","following_url":"https://api.github.com/users/housleyjk/following{/other_user}","gists_url":"https://api.github.com/users/housleyjk/gists{/gist_id}","starred_url":"https://api.github.com/users/housleyjk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/housleyjk/subscriptions","organizations_url":"https://api.github.com/users/housleyjk/orgs","repos_url":"https://api.github.com/users/housleyjk/repos","events_url":"https://api.github.com/users/housleyjk/events{/privacy}","received_events_url":"https://api.github.com/users/housleyjk/received_events","type":"User","site_admin":false},"created_at":"2016-10-29T18:19:38Z","updated_at":"2016-10-29T18:19:38Z","body":"Hopefully this is what you are looking for:\r\n\r\n``` rust\r\nextern crate ws;\r\n\r\nuse std::fmt;\r\nuse std::error::Error as StdError;\r\n\r\n#[derive(Debug)]\r\nstruct MyError;\r\n\r\nimpl StdError for MyError {\r\n\r\n    fn description(&self) -> &str {\r\n        \"my custom error\"\r\n    }\r\n\r\n    fn cause(&self) -> Option<&StdError> {\r\n        None\r\n    }\r\n\r\n}\r\n\r\nimpl fmt::Display for MyError {\r\n\r\n    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {\r\n        write!(f, \"{}\", self.description())\r\n    }\r\n}\r\n\r\nstruct Server {\r\n    out: ws::Sender,\r\n}\r\n\r\nimpl ws::Handler for Server {\r\n\r\n    fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {\r\n        Err(ws::Error::new(ws::ErrorKind::Custom(Box::new(MyError)), \"some custom details\"))\r\n    }\r\n\r\n    fn on_error(&mut self, err: ws::Error) {\r\n        if let ws::ErrorKind::Custom(e) = err.kind {\r\n            println!(\"{}\", e);\r\n\r\n            // if you have multiple custom errors, you can use\r\n            // if e.is::<MyError2>() {... to differentiate\r\n\r\n            // instead of closing the connection, you could send a message\r\n            // custom errors do nothing except trigger on_error by default\r\n\r\n            if let Err(fail) = self.out.close(ws::CloseCode::Normal) {\r\n                println!(\"Failed to schedule close code after custom error: {}\", fail)\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\nfn main() {\r\n\r\n    ws::listen(\"127.0.0.1:3012\", |out| Server { out: out } ).unwrap();\r\n}\r\n```"}}
{"actor":{"display_login":"housleyjk"}}