Add initial support for error handling
This commit is contained in:
@@ -28,6 +28,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.cloud.function.test.app.Pet;
|
||||
import org.springframework.cloud.function.test.app.PetStoreSpringAppConfig;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -44,7 +45,7 @@ public class RequestResponseTests {
|
||||
|
||||
@BeforeEach
|
||||
public void before() {
|
||||
this.mvc = ProxyMvc.INSTANCE(PetStoreSpringAppConfig.class);
|
||||
this.mvc = ProxyMvc.INSTANCE(PetStoreSpringAppConfig.class, ProxyErrorController.class);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@@ -87,6 +88,23 @@ public class RequestResponseTests {
|
||||
assertThat(pet.getName()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void errorThrownFromMethod() throws Exception {
|
||||
HttpServletRequest request = new ProxyHttpServletRequest(null, "GET", "/pets/2");
|
||||
ProxyHttpServletResponse response = new ProxyHttpServletResponse();
|
||||
mvc.service(request, response);
|
||||
assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
|
||||
assertThat(response.getErrorMessage()).isEqualTo("No such Dog");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void errorUnexpectedWhitelabel() throws Exception {
|
||||
HttpServletRequest request = new ProxyHttpServletRequest(null, "GET", "/pets/2/3/4");
|
||||
ProxyHttpServletResponse response = new ProxyHttpServletResponse();
|
||||
mvc.service(request, response);
|
||||
assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validatePostWithBody() throws Exception {
|
||||
ProxyHttpServletRequest request = new ProxyHttpServletRequest(null, "POST", "/pets/");
|
||||
|
||||
@@ -20,10 +20,13 @@ import java.security.Principal;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
@@ -63,8 +66,10 @@ public class PetsController {
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET)
|
||||
public Pet listPets() {
|
||||
System.out.println("=====> Getting pet by id");
|
||||
public Pet listPets(@PathVariable String petId) {
|
||||
if (petId.equals("2")) {
|
||||
throw new DogNotFoundException();
|
||||
}
|
||||
Pet newPet = new Pet();
|
||||
newPet.setId(UUID.randomUUID().toString());
|
||||
newPet.setBreed(PetData.getRandomBreed());
|
||||
@@ -72,4 +77,9 @@ public class PetsController {
|
||||
newPet.setName(PetData.getRandomName());
|
||||
return newPet;
|
||||
}
|
||||
|
||||
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "No such Dog") // 404
|
||||
public class DogNotFoundException extends RuntimeException {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user