title: Global Exception Handling with Spring Cloud Gateway
body: <p>I am using Spring Cloud Gateway Greenwich.SR1 with Spring Boot 2.1.5. I am trying to create a gateway for my downstream services. Part of the gateway's job is to provide a global error page for downstream requests. When a downstream service returns a HTTP 403 response, I want the gateway to provide a fitting error page. </p>
  
  <p>I am currently using a custom filter like this</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ForbiddenFilterFactory extends AbstractGatewayFilterFactory&lt;Object&gt; {
  
      @Override
      public String name() {
          return "Forbidden";
      }
  
      @Override
      public GatewayFilter apply(Object o) {
          return (exchange, chain) -&gt; chain.filter(exchange).then(
                  Mono.defer(() -&gt; {
                      if (!exchange.getResponse().isCommitted() &amp;&amp;
                              HttpStatus.FORBIDDEN.equals(exchange.getResponse().getStatusCode())) {
                          return Mono.error(new ResponseStatusException(HttpStatus.FORBIDDEN));
                      }
                      return Mono.empty();
                  }));
      }
  }
  </code></pre>
  
  <p>I also have a <code>403.html</code> file in <code>src/main/resources/templates/error/</code> set up.</p>
  
  <p>The problem is that the gateway returns a 403 response with an empty body instead of the content of the html file. During debugging I can see that <code>DefaultErrorWebExceptionHandler</code> creates the correct body in form of a <code>Mono&lt;ServerResponse&gt;</code> but it is never written the the actual response.</p>
  
  <p>Is there a different way to get this to work?</p>