Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit c8edd9e

Browse files
committed
Added Global Exception Handling
1 parent 89a22d1 commit c8edd9e

File tree

1 file changed

+38
-2
lines changed
  • admin-panel/src/main/java/net/cryptic_game/backend/admin

1 file changed

+38
-2
lines changed

admin-panel/src/main/java/net/cryptic_game/backend/admin/Bootstrap.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
package net.cryptic_game.backend.admin;
22

3+
import java.security.Principal;
4+
import java.util.stream.Collectors;
5+
6+
import net.getnova.framework.core.GlobalErrorWebExceptionHandler;
37
import net.getnova.framework.core.NovaBanner;
48
import org.springdoc.core.GroupedOpenApi;
9+
import org.springframework.beans.factory.ObjectProvider;
510
import org.springframework.boot.autoconfigure.SpringBootApplication;
11+
import org.springframework.boot.autoconfigure.web.ResourceProperties;
12+
import org.springframework.boot.autoconfigure.web.ServerProperties;
13+
import org.springframework.boot.autoconfigure.web.WebProperties;
14+
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
615
import org.springframework.boot.builder.SpringApplicationBuilder;
716
import org.springframework.boot.context.properties.EnableConfigurationProperties;
17+
import org.springframework.boot.web.reactive.error.ErrorAttributes;
18+
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
19+
import org.springframework.context.ApplicationContext;
820
import org.springframework.context.annotation.Bean;
21+
import org.springframework.core.annotation.Order;
922
import org.springframework.http.HttpHeaders;
1023
import org.springframework.http.MediaType;
24+
import org.springframework.http.codec.ServerCodecConfigurer;
1125
import org.springframework.security.core.Authentication;
1226
import org.springframework.security.core.annotation.AuthenticationPrincipal;
1327
import org.springframework.web.bind.annotation.GetMapping;
1428
import org.springframework.web.bind.annotation.RestController;
1529
import org.springframework.web.reactive.function.client.WebClient;
30+
import org.springframework.web.reactive.result.view.ViewResolver;
1631
import org.springframework.web.server.WebSession;
1732

18-
import java.security.Principal;
19-
2033
@SpringBootApplication
2134
@RestController
2235
@EnableConfigurationProperties(Config.class)
@@ -61,4 +74,27 @@ GroupedOpenApi serverApi() {
6174
.pathsToMatch("/server_management/**")
6275
.build();
6376
}
77+
78+
@Bean
79+
@Order(-2)
80+
public ErrorWebExceptionHandler errorWebExceptionHandler(
81+
final ErrorAttributes errorAttributes,
82+
final ResourceProperties resourceProperties,
83+
final WebProperties webProperties,
84+
final ApplicationContext applicationContext,
85+
final ServerProperties serverProperties,
86+
final ObjectProvider<ViewResolver> viewResolvers,
87+
final ServerCodecConfigurer serverCodecConfigurer
88+
) {
89+
final AbstractErrorWebExceptionHandler exceptionHandler = new GlobalErrorWebExceptionHandler(
90+
errorAttributes,
91+
resourceProperties.hasBeenCustomized() ? resourceProperties : webProperties.getResources(),
92+
applicationContext,
93+
serverProperties.getError()
94+
);
95+
exceptionHandler.setViewResolvers(viewResolvers.orderedStream().collect(Collectors.toList()));
96+
exceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
97+
exceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
98+
return exceptionHandler;
99+
}
64100
}

0 commit comments

Comments
 (0)