Skip to content

Commit

Permalink
refactor: head tag injection to skip error pages
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Sep 26, 2024
1 parent f6409a0 commit 27060ee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.context.MessageSource;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ServerWebExchange;

/**
* See {@link DefaultErrorAttributes} for more.
*
* @author johnn
*/
public class ProblemDetailErrorAttributes extends DefaultErrorAttributes {
public static final String ERROR_INTERNAL_ATTRIBUTE =
ProblemDetailErrorAttributes.class.getName() + ".ERROR";

private final MessageSource messageSource;

Expand All @@ -31,4 +34,9 @@ public Map<String, Object> getErrorAttributes(ServerRequest request,
return errAttributes;
}

@Override
public void storeErrorInformation(Throwable error, ServerWebExchange exchange) {
super.storeErrorInformation(error, exchange);
exchange.getAttributes().putIfAbsent(ERROR_INTERNAL_ATTRIBUTE, error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.thymeleaf.processor.element.IElementModelStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;
import reactor.core.publisher.Flux;
import run.halo.app.infra.exception.handlers.ProblemDetailErrorAttributes;
import run.halo.app.plugin.extensionpoint.ExtensionGetter;

/**
Expand Down Expand Up @@ -43,6 +44,12 @@ public GlobalHeadInjectionProcessor(final String dialectPrefix) {
protected void doProcess(ITemplateContext context, IModel model,
IElementModelStructureHandler structureHandler) {

var error = context.getVariable(ProblemDetailErrorAttributes.ERROR_INTERNAL_ATTRIBUTE);
if (error != null) {
// do not process for error page
return;
}

// note that this is important!!
Object processedAlready = context.getVariable(PROCESS_FLAG);
if (processedAlready != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import reactor.core.publisher.Flux;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.exception.handlers.ProblemDetailErrorAttributes;
import run.halo.app.plugin.extensionpoint.ExtensionGetter;

/**
Expand Down Expand Up @@ -48,6 +49,12 @@ public TemplateFooterElementTagProcessor(final String dialectPrefix) {
protected void doProcess(ITemplateContext context, IProcessableElementTag tag,
IElementTagStructureHandler structureHandler) {

var error = context.getVariable(ProblemDetailErrorAttributes.ERROR_INTERNAL_ATTRIBUTE);
if (error != null) {
// do not process for error page
return;
}

IModel modelToInsert = context.getModelFactory().createModel();
/*
* Obtain the Spring application context.
Expand Down

0 comments on commit 27060ee

Please sign in to comment.