diff --git a/runtime/src/main/java/com/tietoevry/quarkus/resteasy/problem/jaxrs/WebApplicationExceptionMapper.java b/runtime/src/main/java/com/tietoevry/quarkus/resteasy/problem/jaxrs/WebApplicationExceptionMapper.java index 45c4f076..a2a40c90 100644 --- a/runtime/src/main/java/com/tietoevry/quarkus/resteasy/problem/jaxrs/WebApplicationExceptionMapper.java +++ b/runtime/src/main/java/com/tietoevry/quarkus/resteasy/problem/jaxrs/WebApplicationExceptionMapper.java @@ -6,6 +6,7 @@ import jakarta.ws.rs.Priorities; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.Response; +import java.util.Optional; /** * Generic exception mapper for JaxRS WebApplicationExceptions - it passes status and message to application/problem response. @@ -22,10 +23,10 @@ protected HttpProblem toProblem(WebApplicationException exception) { .withStatus(status) .withDetail(exception.getMessage()); - exception - .getResponse() - .getHeaders() - .forEach((header, values) -> values.forEach(value -> problem.withHeader(header, value))); + Optional.ofNullable(exception.getResponse().getHeaders()) + .ifPresent(headers -> { + headers.forEach((header, values) -> values.forEach(value -> problem.withHeader(header, value))); + }); return problem.build(); } diff --git a/runtime/src/test/java/com/tietoevry/quarkus/resteasy/problem/jaxrs/WebApplicationExceptionMapperTest.java b/runtime/src/test/java/com/tietoevry/quarkus/resteasy/problem/jaxrs/WebApplicationExceptionMapperTest.java index 51757443..36195890 100644 --- a/runtime/src/test/java/com/tietoevry/quarkus/resteasy/problem/jaxrs/WebApplicationExceptionMapperTest.java +++ b/runtime/src/test/java/com/tietoevry/quarkus/resteasy/problem/jaxrs/WebApplicationExceptionMapperTest.java @@ -3,6 +3,7 @@ import static com.tietoevry.quarkus.resteasy.problem.HttpProblem.MEDIA_TYPE; import static jakarta.ws.rs.core.HttpHeaders.RETRY_AFTER; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; import com.tietoevry.quarkus.resteasy.problem.HttpProblem; import jakarta.ws.rs.RedirectionException; @@ -10,6 +11,8 @@ import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import java.net.URI; +import org.jboss.resteasy.core.Headers; +import org.jboss.resteasy.core.ServerResponse; import org.junit.jupiter.api.Test; class WebApplicationExceptionMapperTest { @@ -65,4 +68,14 @@ void shouldMapRedirectionException() { .isInstanceOf(HttpProblem.class) .hasFieldOrPropertyWithValue("detail", "HTTP 301 Moved Permanently"); } + + @Test + void shouldHandleNullHeaders() { + Headers nullHeaders = null; + WebApplicationException exception = new WebApplicationException( + new ServerResponse(new Object(), 404, nullHeaders)); + + assertThatCode(() -> mapper.toResponse(exception)) + .doesNotThrowAnyException(); + } }