content: package cn.datax.gateway.config;
  
  import cn.datax.gateway.handler.DataGatewayExceptionHandler;
  import org.springframework.beans.factory.ObjectProvider;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  import org.springframework.boot.autoconfigure.condition.SearchStrategy;
  import org.springframework.boot.autoconfigure.web.ResourceProperties;
  import org.springframework.boot.autoconfigure.web.ServerProperties;
  import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
  import org.springframework.boot.web.reactive.error.ErrorAttributes;
  import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.Ordered;
  import org.springframework.core.annotation.Order;
  import org.springframework.http.codec.ServerCodecConfigurer;
  import org.springframework.web.reactive.result.view.ViewResolver;
  
  import java.util.Collections;
  import java.util.List;
  
  @Configuration
  public class DataGatewayErrorConfigure {
  
      private final ServerProperties serverProperties;
      private final ApplicationContext applicationContext;
      private final ResourceProperties resourceProperties;
      private final List<ViewResolver> viewResolvers;
  
      public DataGatewayErrorConfigure(ServerProperties serverProperties,
                                       ResourceProperties resourceProperties,
                                       ObjectProvider<List<ViewResolver>> viewResolversProvider,
                                       ApplicationContext applicationContext) {
          this.serverProperties = serverProperties;
          this.applicationContext = applicationContext;
          this.resourceProperties = resourceProperties;
          this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
      }
  
      @Bean
      @Order(Ordered.HIGHEST_PRECEDENCE)
      public ErrorWebExceptionHandler errorWebExceptionHandler(ServerCodecConfigurer serverCodecConfigurer) {
          DataGatewayExceptionHandler exceptionHandler = new DataGatewayExceptionHandler(
              new DefaultErrorAttributes(
                  this.serverProperties.getError().isIncludeException()),
              this.resourceProperties,
              this.serverProperties.getError(),
              this.applicationContext
          );
          exceptionHandler.setViewResolvers(this.viewResolvers);
          exceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
          exceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
          return exceptionHandler;
      }
  }
  