Add service broker bad request exception

This commit is contained in:
Giang Vo
2018-10-25 15:04:20 +11:00
committed by Roy Clarkson
parent 4f1b9e541d
commit 34b3876916
3 changed files with 46 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerApiVersionException;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerAsyncRequiredException;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerBadRequestException;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerInvalidParametersException;
import org.springframework.cloud.servicebroker.exception.ServiceDefinitionDoesNotExistException;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceDoesNotExistException;
@@ -141,6 +142,12 @@ public class BaseController {
return getErrorResponse(ex.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
}
@ExceptionHandler(ServiceBrokerBadRequestException.class)
public ResponseEntity<ErrorMessage> handleException(ServiceBrokerBadRequestException ex) {
log.debug("Invalid request received: ", ex);
return getErrorResponse(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorMessage> handleException(Exception ex) {
log.debug("Unknown exception handled: ", ex);

View File

@@ -0,0 +1,22 @@
package org.springframework.cloud.servicebroker.exception;
/**
* Thrown to indicate that request is invalid. 400 response code should be returned.
*/
public class ServiceBrokerBadRequestException extends RuntimeException {
private static final long serialVersionUID = 4719676639792071582L;
public ServiceBrokerBadRequestException(String message) {
super(message);
}
public ServiceBrokerBadRequestException(String message, Throwable cause) {
super(message, cause);
}
public ServiceBrokerBadRequestException(Throwable cause) {
super(cause);
}
}

View File

@@ -18,6 +18,7 @@ import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerAsyncRequiredException;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerBadRequestException;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerInvalidParametersException;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceDoesNotExistException;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceExistsException;
@@ -249,6 +250,22 @@ public class ServiceInstanceControllerIntegrationTest extends ControllerIntegrat
.andExpect(jsonPath("$.description", is("invalid parameters description")));
}
@Test
public void createServiceInstanceWithBadRequestFails() throws Exception {
when(serviceInstanceService.createServiceInstance(eq(syncCreateRequest)))
.thenThrow(new ServiceBrokerBadRequestException("invalid request description"));
setupCatalogService(syncCreateRequest.getServiceDefinitionId());
mockMvc.perform(put(buildUrl(syncCreateRequest, false))
.content(DataFixture.toJson(syncCreateRequest))
.header(API_INFO_LOCATION_HEADER, API_INFO_LOCATION)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.description", is("invalid request description")));
}
@Test
public void createServiceInstanceWithInvalidFieldsFails() throws Exception {
when(serviceInstanceService.createServiceInstance(eq(syncCreateRequest)))