From 8a68a59ea50a40b606875c48ffc175accb13ae95 Mon Sep 17 00:00:00 2001 From: John Niang Date: Fri, 9 May 2025 15:11:48 +0800 Subject: [PATCH] Fix potential twice theme route handler invocations (#7419) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area core /milestone 2.20.x #### What this PR does / why we need it: 1. This PR removes duplicate invocations while resolving handler functions of theme. 2. Throw NotFoundException while post was not found. #### Which issue(s) this PR fixes: Fixes https://github.com/halo-dev/halo/issues/7409 #### Does this PR introduce a user-facing change? ```release-note 修复访问不存在的分类或者文章页面时始终抛出异常的问题 ``` --- .../halo/app/theme/router/ThemeCompositeRouterFunction.java | 4 +--- .../run/halo/app/theme/router/factories/PostRouteFactory.java | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/run/halo/app/theme/router/ThemeCompositeRouterFunction.java b/application/src/main/java/run/halo/app/theme/router/ThemeCompositeRouterFunction.java index 1829ef89e..f1b39af50 100644 --- a/application/src/main/java/run/halo/app/theme/router/ThemeCompositeRouterFunction.java +++ b/application/src/main/java/run/halo/app/theme/router/ThemeCompositeRouterFunction.java @@ -55,9 +55,7 @@ public class ThemeCompositeRouterFunction implements RouterFunction> route(@NonNull ServerRequest request) { return Flux.fromIterable(cachedRouters) - .concatMap(routerFunction -> routerFunction.route(request) - .filterWhen(handle -> handle.handle(request).hasElement()) - ) + .concatMap(routerFunction -> routerFunction.route(request)) .next(); } diff --git a/application/src/main/java/run/halo/app/theme/router/factories/PostRouteFactory.java b/application/src/main/java/run/halo/app/theme/router/factories/PostRouteFactory.java index 4cc14ee79..54e661408 100644 --- a/application/src/main/java/run/halo/app/theme/router/factories/PostRouteFactory.java +++ b/application/src/main/java/run/halo/app/theme/router/factories/PostRouteFactory.java @@ -39,6 +39,7 @@ import run.halo.app.core.extension.content.Post; import run.halo.app.extension.MetadataUtil; import run.halo.app.extension.ReactiveExtensionClient; import run.halo.app.extension.index.query.QueryFactory; +import run.halo.app.infra.exception.NotFoundException; import run.halo.app.infra.utils.JsonUtils; import run.halo.app.theme.DefaultTemplateEnum; import run.halo.app.theme.ViewNameResolver; @@ -105,7 +106,8 @@ public class PostRouteFactory implements RouteFactory { HandlerFunction handlerFunction() { return request -> { PostPatternVariable patternVariable = PostPatternVariable.from(request); - return postResponse(request, patternVariable); + return postResponse(request, patternVariable) + .switchIfEmpty(Mono.error(() -> new NotFoundException("Post not found."))); }; }