Actix_web: handle errors in configure()

#1

I have this snippet of code in my application:

    server::new(move || {
        App::with_state(inside_config.clone())
            .middleware(auth::Auth)
            .middleware(middleware::Logger::default())
            .configure(configure_app)
    })
    .bind(....)

The configure method works fine, since the configure_app method returns an App<MyAppConfig>.

Now, I modified the configure_app method so it returns a Result<App<MyAppConfig>, MyCustomError> because I perform some operations that might fail during configuration.

My question is: how can I make it work with the above syntax ?

I would like to avoid doing this:

.configure(|app| configure_app(app).unwrap())
0 Likes

#2

Actix doesn’t look like it has a way to handle fallible functions, so using a closure is you only choice.

0 Likes

#3

I can’t return from a function from a closure, right ? How could I return from the function if configure_app returns an error ?

Ideally, I know it doesn’t work and that’s not how it’s supposed to be done, but I’d like something like this:

.configure(|app| match configure_app(app){
   Ok(config) => config,
   Err(_) => return Err("damned") // I mean, return from function, not from the closure
})
0 Likes

#4

So, I looked at the source code for actix, configure is a method to allow the builder pattern, and only calls the closure passed into it with self. So you could just call the function normally with the app and propogate the result.

...
let app = App::new();
configure_app(app)?
...
0 Likes