How to convert all panic into Result and return it

PressRex profile image
by PressRex
How to convert all panic into Result and return it

Hello
I have this code:

    fn run(args: String)-> Result<Output, String> {
        let mut sh = Command::new("/usr/bin/shqwqw");

        let mut child = sh
            .args(["-c","cat"])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()
            .or(Err("can not execute shell"))?;
        
        let mut stdin = child.stdin.take().expect("can not connect to stdin");
            std::thread::spawn(move || {
                stdin.write_all(args.as_bytes()).expect("err write to stdin");
            });
        let output = child.wait_with_output().expect("err waiting to finish");
        
        Ok(output)
    }

As you can see it return Result
The idea is to convert all panic into meaningful errors and return them as Err("...").
I managed to fix the first one, but I do not understand how to change the rest of expects.
This function must return value or an error.
If my approach was not correct then suggest anything different.

Thank you in advance.

2 posts - 1 participant

Read full topic

Source: View source

PressRex profile image
by PressRex

Subscribe to New Posts

Lorem ultrices malesuada sapien amet pulvinar quis. Feugiat etiam ullamcorper pharetra vitae nibh enim vel.

Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks

Read More