Skip to content

Commit 3c96e7f

Browse files
committed
Separate error handler registration from FunctionEndpointInitializer
1 parent 8a386b3 commit 3c96e7f

2 files changed

Lines changed: 51 additions & 20 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.function.web.function;
18+
19+
import org.springframework.boot.autoconfigure.web.ErrorProperties;
20+
import org.springframework.boot.autoconfigure.web.WebProperties.Resources;
21+
import org.springframework.boot.webflux.autoconfigure.error.DefaultErrorWebExceptionHandler;
22+
import org.springframework.boot.webflux.error.DefaultErrorAttributes;
23+
import org.springframework.boot.webflux.error.ErrorAttributes;
24+
import org.springframework.context.support.GenericApplicationContext;
25+
import org.springframework.http.codec.ServerCodecConfigurer;
26+
27+
public final class ErrorHandlerRegistrar {
28+
29+
private ErrorHandlerRegistrar() {
30+
31+
}
32+
33+
protected static DefaultErrorWebExceptionHandler errorHandler(GenericApplicationContext context) {
34+
context.registerBean(ErrorAttributes.class, () -> new DefaultErrorAttributes());
35+
context.registerBean(ErrorProperties.class, () -> new ErrorProperties());
36+
37+
context.registerBean(Resources.class, () -> new Resources());
38+
DefaultErrorWebExceptionHandler handler = new DefaultErrorWebExceptionHandler(
39+
context.getBeansOfType(ErrorAttributes.class).values().iterator().next(),
40+
context.getBean(Resources.class), context.getBean(ErrorProperties.class), context);
41+
ServerCodecConfigurer codecs = ServerCodecConfigurer.create();
42+
handler.setMessageWriters(codecs.getWriters());
43+
handler.setMessageReaders(codecs.getReaders());
44+
return handler;
45+
}
46+
}

spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/function/FunctionEndpointInitializer.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@
3131
import reactor.netty.http.server.HttpServer;
3232

3333
import org.springframework.boot.WebApplicationType;
34-
import org.springframework.boot.autoconfigure.web.ErrorProperties;
35-
import org.springframework.boot.autoconfigure.web.WebProperties.Resources;
3634
import org.springframework.boot.webflux.autoconfigure.error.DefaultErrorWebExceptionHandler;
37-
import org.springframework.boot.webflux.error.DefaultErrorAttributes;
38-
import org.springframework.boot.webflux.error.ErrorAttributes;
3935
import org.springframework.cloud.function.context.FunctionCatalog;
4036
import org.springframework.cloud.function.context.FunctionProperties;
4137
import org.springframework.cloud.function.context.FunctionalSpringApplication;
@@ -56,7 +52,6 @@
5652
import org.springframework.core.env.Environment;
5753
import org.springframework.core.env.MapPropertySource;
5854
import org.springframework.http.ResponseEntity;
59-
import org.springframework.http.codec.ServerCodecConfigurer;
6055
import org.springframework.http.server.reactive.HttpHandler;
6156
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
6257
import org.springframework.util.Assert;
@@ -92,7 +87,7 @@ public class FunctionEndpointInitializer implements ApplicationContextInitialize
9287

9388
@Override
9489
public void initialize(GenericApplicationContext context) {
95-
if (errorAttributes && webflux && ContextFunctionCatalogInitializer.enabled
90+
if (webflux && ContextFunctionCatalogInitializer.enabled
9691
&& context.getEnvironment().getProperty(FunctionalSpringApplication.SPRING_WEB_APPLICATION_TYPE,
9792
WebApplicationType.class, WebApplicationType.REACTIVE) == WebApplicationType.REACTIVE
9893
&& context.getEnvironment().getProperty("spring.functional.enabled", Boolean.class, false)) {
@@ -102,7 +97,9 @@ public void initialize(GenericApplicationContext context) {
10297
}
10398

10499
private void registerWebFluxAutoConfiguration(GenericApplicationContext context) {
105-
context.registerBean(DefaultErrorWebExceptionHandler.class, () -> errorHandler(context));
100+
if (errorAttributes) {
101+
context.registerBean(DefaultErrorWebExceptionHandler.class, () -> ErrorHandlerRegistrar.errorHandler(context));
102+
}
106103
context.registerBean(WebHttpHandlerBuilder.WEB_HANDLER_BEAN_NAME, HttpWebHandlerAdapter.class,
107104
() -> httpHandler(context));
108105
context.addApplicationListener(new ServerListener(context));
@@ -122,19 +119,7 @@ private HttpWebHandlerAdapter httpHandler(GenericApplicationContext context) {
122119
.codecs(config -> config.registerDefaults(true)).build());
123120
}
124121

125-
private DefaultErrorWebExceptionHandler errorHandler(GenericApplicationContext context) {
126-
context.registerBean(ErrorAttributes.class, () -> new DefaultErrorAttributes());
127-
context.registerBean(ErrorProperties.class, () -> new ErrorProperties());
128-
129-
context.registerBean(Resources.class, () -> new Resources());
130-
DefaultErrorWebExceptionHandler handler = new DefaultErrorWebExceptionHandler(
131-
context.getBeansOfType(ErrorAttributes.class).values().iterator().next(), context.getBean(Resources.class),
132-
context.getBean(ErrorProperties.class), context);
133-
ServerCodecConfigurer codecs = ServerCodecConfigurer.create();
134-
handler.setMessageWriters(codecs.getWriters());
135-
handler.setMessageReaders(codecs.getReaders());
136-
return handler;
137-
}
122+
138123

139124
private static final class RouterFunctionRegister {
140125

0 commit comments

Comments
 (0)