Skip to content

Commit

Permalink
fix NullPointerException that would occur when WebApplicationExceptio…
Browse files Browse the repository at this point in the history
…n was thrown with an unassigned Http Status Code
  • Loading branch information
JacobOJ authored and JacobOJ committed Nov 19, 2024
1 parent 8fae340 commit e263dba
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static jakarta.ws.rs.core.Response.Status.NOT_FOUND;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.nullValue;

import io.quarkus.test.junit.QuarkusTest;
Expand Down Expand Up @@ -40,13 +41,13 @@ void webApplicationExceptionShouldReturnGivenStatus(int status) {

@ParameterizedTest(name = "http status: {0}")
@ValueSource(ints = { 318, 499, 533 })
void webApplicationExceptionWithInvalidHttpStatusCodeShouldNotFail(int status) {
void webApplicationExceptionWithUnassignedHttpStatusCodeShouldNotFail(int status) {
given()
.queryParam("status", status)
.get("/throw/jax-rs/web-application-exception")
.then()
.statusCode(status)
.body("title", equalTo("Unknown HTTP Status Code"))
.body("title", equalToIgnoringCase("Unknown Code"))
.body("status", equalTo(status))
.body("stacktrace", nullValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public final class WebApplicationExceptionMapper extends ExceptionMapperBase<Web

@Override
protected HttpProblem toProblem(WebApplicationException exception) {
Response.StatusType status = exception.getResponse().getStatusInfo();

HttpProblem.Builder problem = HttpProblem.builder()
.withTitle(getTitle(exception.getResponse().getStatus()))
.withStatus(exception.getResponse().getStatus())
.withTitle(status.getReasonPhrase())
.withStatus(status)
.withDetail(exception.getMessage());

Optional.ofNullable(exception.getResponse().getHeaders())
Expand All @@ -30,13 +32,4 @@ protected HttpProblem toProblem(WebApplicationException exception) {

return problem.build();
}

private static String getTitle(int statusCode) {
Response.StatusType status = Response.Status.fromStatusCode(statusCode);

return status != null
? status.getReasonPhrase()
: "Unknown HTTP Status Code";
}

}

0 comments on commit e263dba

Please sign in to comment.