spring-cloud / spring-cloud-open-service-broker

Spring Cloud project for creating service brokers that conform to the Open Server Broker API specification
https://spring.io/projects/spring-cloud-open-service-broker
Apache License 2.0
168 stars 118 forks source link

How to return correct error HTTP code instead of an Exception when checking for missing header "accepts_incomplete=true" #334

Closed AbrahamArellano closed 3 years ago

AbrahamArellano commented 3 years ago

Hi team,

I would need your support in order to understand how to return the right HTTP error code instead of "500" when the header: accepts_incomplete=true, is missing.

https://github.com/openservicebrokerapi/servicebroker/blob/master/spec.md#asynchronous-operations

Our current Service implementation inherits from "org.springframework.cloud.servicebroker.service.ServiceInstanceService" and we have no custom controller. However when we throw a Mono.error(new ServiceBrokerAsyncRequiredException()) we received on client a 500 error.

public Mono<CreateServiceInstanceResponse> createServiceInstance(CreateServiceInstanceRequest request) {
    try {
      logger.info("Entering method {} parameter {}", "createServiceInstance", request);
      checkIfmissingHeader(request); // this method throws an ServiceBrokerAsyncRequiredException.
      if (this.serviceInstanceRepository.existsByServiceInstanceId(request.getServiceInstanceId())) {
        return Mono.just(
          CreateServiceInstanceResponse.builder()
            .async(true)
            .instanceExisted(true)
            .build()
        );
      }
      ServiceInstance serviceInstance = this.toServiceInstance(request);
      this.serviceInstanceService.saveServiceInstance(serviceInstance);
      this.serviceInstanceService.provisionServiceInstance(serviceInstance);
      return Mono.just(
        CreateServiceInstanceResponse.builder()
          .async(true)
          .build()
      );
    } catch (RuntimeException ex) {
      return Mono.error(ex); 
    }
AbrahamArellano commented 3 years ago

The problem the WebTest class which was instantiated instead of injected causing the tests to fail. Real tests returned the right HTTP code.