1
0

return errors from handlers

This commit is contained in:
Ishan Jain 2024-01-04 00:51:52 +05:30
parent b7728d43c5
commit 94c2e1a46b
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
2 changed files with 31 additions and 28 deletions

View File

@ -10,7 +10,7 @@ use std::{
pub struct Malestorm<'a> { pub struct Malestorm<'a> {
node: RefCell<Node>, node: RefCell<Node>,
io: MalestormIo, io: MalestormIo,
pub handlers: HashMap<&'a str, Box<dyn Fn(Message, Rc<Malestorm>)>>, pub handlers: HashMap<&'a str, Box<dyn Fn(Message, Rc<Malestorm>) -> Result<(), String>>>,
} }
struct Node { struct Node {
@ -96,7 +96,9 @@ impl<'a> Malestorm<'a> {
} }
}; };
handler(message, pc); if let Err(e) = handler(message, pc) {
eprintln!("error in serving request: {}", e);
}
} }
} }
@ -113,7 +115,7 @@ impl<'a> Malestorm<'a> {
pub fn register( pub fn register(
&mut self, &mut self,
name: &'a str, name: &'a str,
func: impl Fn(Message, Rc<Malestorm>) + 'a + 'static, func: impl Fn(Message, Rc<Malestorm>) -> Result<(), String> + 'a + 'static,
) { ) {
self.handlers.insert(name, Box::new(func)); self.handlers.insert(name, Box::new(func));
} }

View File

@ -7,37 +7,38 @@ use std::rc::Rc;
fn main() { fn main() {
let mut program = Malestorm::default(); let mut program = Malestorm::default();
program.register("init", |mut msg: Message, program: Rc<Malestorm>| { program.register(
let message_body: InitMessage = serde_json::from_value(msg.body.message_body.take()) "init",
.expect("error in parsing message body"); // TODO: Replace error string with a type
|mut msg: Message, program: Rc<Malestorm>| -> Result<(), String> {
let message_body: InitMessage = serde_json::from_value(msg.body.message_body.take())
.map_err(|e| format!("error in parsing response body: {}", e))?;
program.set_node_id(message_body.node_id); program.set_node_id(message_body.node_id);
program.set_nodes(message_body.nodes); program.set_nodes(message_body.nodes);
msg.body.message_type = "init_ok".into(); msg.body.message_type = "init_ok".into();
match program.reply(msg) { program
Ok(_) => (), .reply(msg)
Err(e) => { .map_err(|e| format!("init: error in writing response: {}", e))
eprintln!("init: error in writing response: {}", e); },
} );
}
});
program.register("echo", |mut msg: Message, program: Rc<Malestorm>| { program.register(
let message_body: EchoMessage = serde_json::from_value(msg.body.message_body.take()) "echo",
.expect("error in parsing message body"); |mut msg: Message, program: Rc<Malestorm>| -> Result<(), String> {
let message_body: EchoMessage = serde_json::from_value(msg.body.message_body.take())
.map_err(|e| format!("error in parsing response body: {}", e))?;
msg.body.message_type = "echo_ok".into(); msg.body.message_type = "echo_ok".into();
msg.body.message_body = serde_json::to_value(message_body).unwrap(); msg.body.message_body = serde_json::to_value(message_body).unwrap();
match program.reply(msg) { program
Ok(_) => (), .reply(msg)
Err(e) => { .map_err(|e| format!("echo: error in writing response: {}", e))
eprintln!("echo: error in writing response: {}", e); },
} );
}
});
program.run(); program.run();
} }