miketang84 / sapper

A lightweight web framework built on hyper, implemented in Rust language.
MIT License
617 stars 46 forks source link

add CustomResponse for Error #29

Closed DCjanus closed 6 years ago

DCjanus commented 6 years ago

there something like

fn do_something() -> Option<Info> {
    // some code may return None
}

fn custom_response() -> Response {
    // some code build custom response
}

with this commit, you could write code like

fn with_custom_response_error(req: &mut Request, res: Response) -> SapperResult<Response> {
    let info = do_something().ok_or(SapperError::CustomResponse(custom_response()))?; // success or return custom response immediately
    // some code
    Ok(res)
}

without this commit, you have to write code like

fn without_custom_response_error(req: &mut Request, res: Response) -> SapperResult<Response> {
    if let Some(info) = do_something() {
        // some code
        Ok(res)
    } else {
        Ok(custom_response())
    }
}
DCjanus commented 6 years ago

Hope to be merged soon @daogangtang @driftluo

With custom error, there will be more option for middleware.