I want to return an error from a function in case a condition is true:
use std::error::Error;
pub fn run() -> Result<(), Box> {
//
I am new to Rust, but here is my dirty hack to return custom errors, given that the function is set to return Result<(), Box
:
fn serve(config: &Config, stream: TcpStream) -> Result<(), Box> {
// ...
if request_is_bad() {
// This returns immediately a custom "Bad request" error
Err("Bad request")?;
}
// ...
}