Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webmvc.fn onError doesn't work with CompletableFuture #26831

Closed
dudkiewicz-codes opened this issue Apr 20, 2021 · 0 comments
Closed

webmvc.fn onError doesn't work with CompletableFuture #26831

dudkiewicz-codes opened this issue Apr 20, 2021 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@dudkiewicz-codes
Copy link

dudkiewicz-codes commented Apr 20, 2021

Affects: 5.3.6

webmvc.fn onError doesn't work with CompletableFuture.
Error handler predicates are never called.
Take a look at the code below.
error1 scenario shows the problem: application should return an error response with "error" body, returns Whitelabel Error Page instead.
error2 scenario works fine.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerResponse;

import java.util.concurrent.CompletableFuture;

import static org.springframework.web.servlet.function.RouterFunctions.route;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public RouterFunction<ServerResponse> routes() {
        return route()
            .GET("error1", request -> {
                CompletableFuture<String> result = CompletableFuture.supplyAsync(
                    () -> {
                        throwRuntimeException();
                        return "test";
                    }
                );
                return ServerResponse.ok().body(result);
            })
            .GET("error2", request -> {
                throwRuntimeException();
                CompletableFuture<String> result = CompletableFuture.supplyAsync(
                    () -> "test"
                );
                return ServerResponse.ok().body(result);
            })
            .onError(RuntimeException.class, (throwable, request) ->
                ServerResponse
                    .status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("error")
            )
            .build();
    }

    private void throwRuntimeException() {
        throw new RuntimeException();
    }


}

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 20, 2021
@poutsma poutsma self-assigned this Apr 22, 2021
@poutsma poutsma added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 29, 2021
@poutsma poutsma added this to the 5.3.7 milestone Apr 29, 2021
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this issue Aug 20, 2021
This commit makes sure that any error handlers registered on the route
are also applied when an error occurs asynchronously. This commit
applies to asynchronous bodies with both CompletableFuture and Reactive
Streams, as well as completely asynchronous responses.

Closes spring-projectsgh-26831
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
This commit makes sure that any error handlers registered on the route
are also applied when an error occurs asynchronously. This commit
applies to asynchronous bodies with both CompletableFuture and Reactive
Streams, as well as completely asynchronous responses.

Closes spring-projectsgh-26831
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants