Skip to content

Commit

Permalink
Redirect 404s to frontend
Browse files Browse the repository at this point in the history
Addresses RISDEV-2956
  • Loading branch information
zechmeister committed Dec 29, 2023
1 parent 9991b5a commit dfda2f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.bund.digitalservice.ris.caselaw.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.WebExceptionHandler;

@Configuration
public class ErrorHandlerConfig {

@Bean
public WebExceptionHandler forwardingExceptionHandler() {
return new ForwardingWebExceptionHandler();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.bund.digitalservice.ris.caselaw.config;

import java.net.URI;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;

public class ForwardingWebExceptionHandler implements WebExceptionHandler {

@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof ResponseStatusException) {

This comment has been minimized.

Copy link
@rvp-c

rvp-c Jan 16, 2024

Contributor

You can put a variable name behind the ResponseStatusException and don't need the cast in the next line.
if (ex instanceof ResponseStatusException rse) {

HttpStatusCode status = ((ResponseStatusException) ex).getStatusCode();
if (status == HttpStatus.NOT_FOUND) {
exchange.getResponse().setStatusCode(HttpStatus.FOUND);
exchange.getResponse().getHeaders().setLocation(URI.create("/404.index"));
return exchange.getResponse().setComplete();
}
}
return Mono.error(ex);
}
}

0 comments on commit dfda2f4

Please sign in to comment.