Skip to content

Commit

Permalink
fix: don’t throw NPE if exception message is null (#11591)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo authored Feb 14, 2025
1 parent 1563914 commit 199c2e8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.i18n.ResourceBundleMessageSource;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.*;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.annotation.Status;
import io.micronaut.http.annotation.*;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.http.server.exceptions.ErrorResponseProcessorExceptionHandler;
import io.micronaut.http.server.exceptions.ExceptionHandler;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.annotation.security.PermitAll;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -100,6 +100,14 @@ void validationErrorsShowInHtmlErrorPages() {
assertExpectedSubstringInHtml("Es posible que haya escrito mal la dirección o que la página se haya movido.", html);
}

@Test
void userNotFoundExceptionHandlerReturnsNotFound(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
HttpClientResponseException ex = assertThrows(HttpClientResponseException.class,
() -> client.exchange(HttpRequest.GET("/throws").accept(MediaType.TEXT_HTML)));
assertEquals(HttpStatus.NOT_FOUND, ex.getStatus());
}

private void assertExpectedSubstringInHtml(String expected, String html) {
if (!html.contains(expected)) {
LOG.trace("{}", html);
Expand Down Expand Up @@ -131,4 +139,34 @@ MessageSource createMessageSource() {
@Introspected
record Book(@NotBlank String title, @NotBlank String author, @Max(4032) int pages) {
}

static class UserNotFoundException extends RuntimeException {
}

@Requires(property = "spec.name", value = "DefaultHtmlBodyErrorResponseProviderTest")
@Produces
@Singleton
@Requires(classes = {UserNotFoundException.class, ExceptionHandler.class})
static class UserNotFoundExceptionHandler extends ErrorResponseProcessorExceptionHandler<UserNotFoundException> {
protected UserNotFoundExceptionHandler(ErrorResponseProcessor<?> responseProcessor) {
super(responseProcessor);
}

@Override
protected @NonNull MutableHttpResponse<?> createResponse(UserNotFoundException exception) {
return HttpResponse.notFound();
}
}

@Requires(property = "spec.name", value = "DefaultHtmlBodyErrorResponseProviderTest")
@Controller("/throws")
static class ThrowingController {
@Produces(MediaType.TEXT_HTML)
@PermitAll
@Get
@Status(HttpStatus.OK)
void index() {
throw new UserNotFoundException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.util.LocaleResolver;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
Expand Down Expand Up @@ -177,7 +178,7 @@ private HtmlErrorPage error(@NonNull ErrorContext errorContext,

List<String> messages = new ArrayList<>();
for (io.micronaut.http.server.exceptions.response.Error e : errorContext.getErrors()) {
if (!e.getMessage().equalsIgnoreCase(httpStatusReason)) {
if (StringUtils.isNotEmpty(e.getMessage()) && !e.getMessage().equalsIgnoreCase(httpStatusReason)) {
messages.add(htmlSanitizer.sanitize(e.getMessage()));
}
}
Expand Down

0 comments on commit 199c2e8

Please sign in to comment.