generated from digitalservicebund/java-application-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/de/bund/digitalservice/ris/caselaw/config/ErrorHandlerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...rc/main/java/de/bund/digitalservice/ris/caselaw/config/ForwardingWebExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
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); | ||
} | ||
} |
You can put a variable name behind the ResponseStatusException and don't need the cast in the next line.
if (ex instanceof ResponseStatusException rse) {