Skip to content

Commit

Permalink
[#343] Fix for: NullpointerException WebApplicationException when Res…
Browse files Browse the repository at this point in the history
…ponse Headers not set (#348)
  • Loading branch information
lwitkowski authored Jan 29, 2024
1 parent 5791321 commit 7dc6760
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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;
import jakarta.ws.rs.WebApplicationException;
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 {
Expand Down Expand Up @@ -65,4 +68,14 @@ void shouldMapRedirectionException() {
.isInstanceOf(HttpProblem.class)
.hasFieldOrPropertyWithValue("detail", "HTTP 301 Moved Permanently");
}

@Test
void shouldHandleNullHeaders() {
Headers<Object> nullHeaders = null;
WebApplicationException exception = new WebApplicationException(
new ServerResponse(new Object(), 404, nullHeaders));

assertThatCode(() -> mapper.toResponse(exception))
.doesNotThrowAnyException();
}
}

0 comments on commit 7dc6760

Please sign in to comment.