Chris
Chris

Reputation: 40641

Returning errors from a rust closure and getting the types satisfied

I'm having trouble getting a closure to return the correct type when using the lambda_http crate. My redacted code looks like:

use lambda_http::{lambda, IntoResponse, Request, Response};
use lambda_runtime::{error::HandlerError, Context};
use std::error::Error;
use system_etrainingoz::{handle_request, HandlerRequest};

fn main() -> Result<(), Box<dyn Error>> {
    lambda!(|e: Request, _c: Context| {
        let mut response = Response::builder();
        ... my_body is std::vec::Vec<u8>
        return response.body(my_body);   <-- Problem is here.
        // return response.body(my_body)?; <-- This does not work either.
    });
}

The error seems to be that there are several versions of the lambda! macro, one of which supports returning errors and one that doesnt, and I cannot get my return error types to match up as expected. response.body returns Result<Response> thus can error. The compile errors are as follows:

error[E0271]: type mismatch resolving `<[closure@src/bin/lambda.rs:13:13: 27:6] as FnOnce<(lambda_http::http::Request<Body>, lambda_runtime::Context)>>::Output == std::result::Result<_, HandlerError>`
  --> src/bin/lambda.rs:13:5
    |
13  | /     lambda!(|e: Request, _c: Context| {
14  | |         let (parts, body) = e.into_parts(); 
15  | |         let handler_request = HandlerRequest {
16  | |             parts: parts,
...   |
26  | |         // return response.body(handled_response.body)?;
27  | |     });
    | |_______^ expected struct `lambda_http::http::Error`, found struct `HandlerError`
    | 
  ::: /Users/chris/.cargo/registry/src/github.com-1ecc6299db9ec823/lambda_http-0.1.1/src/lib.rs:100:25
    |
100 |   pub fn start<R>(f: impl Handler<R>, runtime: Option<TokioRuntime>)
    |                           ---------- required by this bound in `lambda_http::start`
    |
    = note: expected enum `std::result::Result<Response<Vec<u8>>, lambda_http::http::Error>`
              found enum `std::result::Result<_, HandlerError>`
    = note: required because of the requirements on the impl of `lambda_http::Handler<_>` for `[closure@src/bin/lambda.rs:13:13: 27:6]`
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Any suggestions appreciated :)

Upvotes: 1

Views: 1092

Answers (1)

Netwave
Netwave

Reputation: 42746

Error types differ, just map the error to get the actually needed type:

fn main() -> Result<(), Box<dyn Error>> {
    lambda!(|e: Request, _c: Context| {
        let mut response = Response::builder();
        let my_body = Vec::new();
        response.body(my_body).map_err(|err| {
            HandlerError::from(
                format!("Actually use your error instead of this one: {:?}", err).as_str(),
            )
        })
    });
    Ok(())
}

Also implementing From may be interesting too for mapping error types.

Upvotes: 3

Related Questions