diff --git a/application/src/main/java/run/halo/app/infra/exception/handlers/ProblemDetailErrorAttributes.java b/application/src/main/java/run/halo/app/infra/exception/handlers/ProblemDetailErrorAttributes.java index 9329b9e01..68d3dd765 100644 --- a/application/src/main/java/run/halo/app/infra/exception/handlers/ProblemDetailErrorAttributes.java +++ b/application/src/main/java/run/halo/app/infra/exception/handlers/ProblemDetailErrorAttributes.java @@ -8,6 +8,7 @@ import java.time.Instant; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; +import java.util.Optional; import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.reactive.error.DefaultErrorAttributes; import org.springframework.boot.web.reactive.error.ErrorAttributes; @@ -39,7 +40,7 @@ public class ProblemDetailErrorAttributes implements ErrorAttributes { @Override public Map getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { - var errAttributes = new LinkedHashMap(); + final var errAttributes = new LinkedHashMap(); var error = getError(request); var responseStatusAnno = from(error.getClass(), SearchStrategy.TYPE_HIERARCHY) @@ -50,8 +51,12 @@ public class ProblemDetailErrorAttributes implements ErrorAttributes { if (error instanceof ErrorResponse er) { errorResponse = er; } else { - var reason = responseStatusAnno.getValue("reason", String.class) - .orElse(error.getMessage()); + var reason = Optional.of(status) + .filter(HttpStatusCode::is5xxServerError) + .map(s -> "Something went wrong, please try again later.") + .orElseGet(() -> responseStatusAnno.getValue("reason", String.class) + .orElse(error.getMessage()) + ); errorResponse = ErrorResponse.create(error, status, reason); } var problemDetail = diff --git a/application/src/main/java/run/halo/app/theme/ThemeConfiguration.java b/application/src/main/java/run/halo/app/theme/ThemeConfiguration.java index edb27abd6..7f38e8091 100644 --- a/application/src/main/java/run/halo/app/theme/ThemeConfiguration.java +++ b/application/src/main/java/run/halo/app/theme/ThemeConfiguration.java @@ -46,6 +46,9 @@ public class ThemeConfiguration { var resource = request.pathVariable("resource"); resource = StringUtils.removeStart(resource, "/"); var fsRes = new FileSystemResource(getThemeAssetsPath(themeName, resource)); + if (!fsRes.exists()) { + return ServerResponse.notFound().build(); + } var bodyBuilder = ServerResponse.ok() .cacheControl(cacheProperties.getCachecontrol().toHttpCacheControl()); try { diff --git a/application/src/main/java/run/halo/app/theme/endpoint/CommentFinderEndpoint.java b/application/src/main/java/run/halo/app/theme/endpoint/CommentFinderEndpoint.java index 8f0316f06..3cfd091ee 100644 --- a/application/src/main/java/run/halo/app/theme/endpoint/CommentFinderEndpoint.java +++ b/application/src/main/java/run/halo/app/theme/endpoint/CommentFinderEndpoint.java @@ -22,6 +22,7 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; +import org.springframework.web.server.ServerWebInputException; import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; import run.halo.app.content.comment.CommentRequest; @@ -230,7 +231,7 @@ public class CommentFinderEndpoint implements CustomEndpoint { public String getKind() { String kind = emptyToNull(queryParams.getFirst("kind")); if (kind == null) { - throw new IllegalArgumentException("The kind must not be null."); + throw new ServerWebInputException("The kind must not be null."); } return kind; } @@ -244,7 +245,7 @@ public class CommentFinderEndpoint implements CustomEndpoint { public String getName() { String name = emptyToNull(queryParams.getFirst("name")); if (name == null) { - throw new IllegalArgumentException("The name must not be null."); + throw new ServerWebInputException("The name must not be null."); } return name; } diff --git a/application/src/test/java/run/halo/app/infra/exception/handlers/I18nExceptionTest.java b/application/src/test/java/run/halo/app/infra/exception/handlers/I18nExceptionTest.java index 161a3be78..c65c78c6f 100644 --- a/application/src/test/java/run/halo/app/infra/exception/handlers/I18nExceptionTest.java +++ b/application/src/test/java/run/halo/app/infra/exception/handlers/I18nExceptionTest.java @@ -105,13 +105,14 @@ class I18nExceptionTest { @Test void shouldGetErrorIfThrowingGeneralException() { + // problem reason will be a fixed prompt when internal server error occurred. webClient.get().uri("/response-entity/general-error") .exchange() .expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR) .expectBody(ProblemDetail.class) .value(problemDetail -> { assertEquals("Internal Server Error", problemDetail.getTitle()); - assertEquals("Something went wrong", + assertEquals("Something went wrong, please try again later.", problemDetail.getDetail()); }); }