Skip to content

Commit

Permalink
Merge pull request #5395 from geoand/#5392
Browse files Browse the repository at this point in the history
Make @ResponseStatus on a void controller advice method optional
  • Loading branch information
gsmet committed Nov 13, 2019
2 parents 76f0d79 + c4cd39c commit 67c1b76
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
Expand Up @@ -108,16 +108,14 @@ protected void preGenerateMethodBody(ClassCreator cc) {
void generateMethodBody(MethodCreator toResponse) {
if (returnType.kind() == Type.Kind.VOID) {
AnnotationInstance responseStatusInstance = controllerAdviceMethod.annotation(RESPONSE_STATUS);
if (responseStatusInstance == null) {
throw new IllegalStateException(
"void methods annotated with @ExceptionHandler must also be annotated with @ResponseStatus");
}

// invoke the @ExceptionHandler method
exceptionHandlerMethodResponse(toResponse);

// build a JAX-RS response
ResultHandle status = toResponse.load(getHttpStatusFromAnnotation(responseStatusInstance));
ResultHandle status = toResponse
.load(responseStatusInstance != null ? getHttpStatusFromAnnotation(responseStatusInstance)
: Response.Status.NO_CONTENT.getStatusCode());
ResultHandle responseBuilder = toResponse.invokeStaticMethod(
MethodDescriptor.ofMethod(Response.class, "status", Response.ResponseBuilder.class, int.class),
status);
Expand Down
Expand Up @@ -403,12 +403,6 @@ private void generateMappersForExceptionHandlerInControllerAdvice(
reflectiveClassProducer.produce(new ReflectiveClassBuildItem(true, true, returnTypeDotName.toString()));
}

AnnotationInstance responseStatusInstance = method.annotation(RESPONSE_STATUS);
if ((method.returnType().kind() == Type.Kind.VOID) && (responseStatusInstance == null)) {
throw new IllegalStateException(
"void methods annotated with @ExceptionHandler must also be annotated with @ResponseStatus");
}

// we need to generate one JAX-RS ExceptionMapper per Exception type
Type[] handledExceptionTypes = exceptionHandlerInstance.value().asClassArray();
for (Type handledExceptionType : handledExceptionTypes) {
Expand Down
Expand Up @@ -18,6 +18,11 @@ public void handleRuntimeException() {

}

@ExceptionHandler(UnannotatedException.class)
public void unannotatedException() {

}

@ExceptionHandler(IllegalStateException.class)
public ResponseEntity<Error> handleIllegalStateException(IllegalStateException e,
HttpServletRequest request, HttpServletResponse response) {
Expand Down
Expand Up @@ -24,6 +24,11 @@ public void runtimeException() {
throw new RuntimeException();
}

@GetMapping("/unannotated")
public void unannotated() {
throw new UnannotatedException();
}

@GetMapping("/responseEntity")
public Greeting handledByResponseEntity() {
throw new IllegalStateException("bad state");
Expand Down
@@ -0,0 +1,4 @@
package io.quarkus.it.spring.web;

public class UnannotatedException extends RuntimeException {
}
Expand Up @@ -69,6 +69,14 @@ public void testExceptionHandlerVoidReturnType() {
.statusCode(400);
}

@Test
public void testExceptionHandlerWithoutResponseStatusOnExceptionOrMethod() {
RestAssured.when().get("/exception/unannotated").then()
.contentType("text/plain")
.body(isEmptyString())
.statusCode(204);
}

@Test
public void testExceptionHandlerResponseEntityType() {
RestAssured.when().get("/exception/responseEntity").then()
Expand Down

0 comments on commit 67c1b76

Please sign in to comment.