Skip to content

Commit

Permalink
added application error catcher filter
Browse files Browse the repository at this point in the history
  • Loading branch information
emmdurin committed May 23, 2024
1 parent 1995564 commit 19edfd6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/custom-error-pages.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,25 @@ spring:
3. In the datadir create a folder from the root directory: "gateway/templates/error"
4. Place your error page files named as per the status code. For example for 404: 404.html
5. Restart georchestra gateway.

== Using custom error pages for applications errors

Custom error pages can also be used when an application behind the gateways returns an error.

To enable it globally, add this to application.yaml :
[application.yaml]
----
spring:
cloud:
gateway:
default-filters:
- ServiceError
----

To enable it only on some routes, add this to concerned routes in routes.yaml :
[routes.yaml]
----
filters:
- name: ServiceError
----

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.georchestra.gateway.filter.global.ResolveTargetGlobalFilter;
import org.georchestra.gateway.filter.headers.HeaderFiltersConfiguration;
import org.georchestra.gateway.filter.global.ServiceErrorGatewayFilterFactory;
import org.georchestra.gateway.model.GatewayConfigProperties;
import org.georchestra.gateway.model.GeorchestraTargetConfig;
import org.geoserver.cloud.gateway.filter.RouteProfileGatewayFilterFactory;
Expand Down Expand Up @@ -64,4 +65,8 @@ public class FiltersAutoConfiguration {
public @Bean StripBasePathGatewayFilterFactory stripBasePathGatewayFilterFactory() {
return new StripBasePathGatewayFilterFactory();
}

public @Bean ServiceErrorGatewayFilterFactory serviceErrorGatewayFilterFactory() {
return new ServiceErrorGatewayFilterFactory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.georchestra.gateway.filter.global;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class ServiceErrorGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
public ServiceErrorGatewayFilterFactory() {
super(Object.class);
}

@Override
public GatewayFilter apply(final Object config) {
return new ServiceErrorGatewayFilter();
}

private static class ServiceErrorGatewayFilter implements GatewayFilter, Ordered {

public @Override Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
HttpStatus statusCode = exchange.getResponse().getStatusCode();
if (statusCode.is4xxClientError() || statusCode.is5xxServerError()) {
throw new ResponseStatusException(statusCode);
}
}));
}

@Override
public int getOrder() {
return ResolveTargetGlobalFilter.ORDER + 1;
}
}
}

0 comments on commit 19edfd6

Please sign in to comment.