0

I am trying to introduce a custom decoder in order to throw appropriate exceptions rather than the generic Feign.Retryable exception and for that I wanted a custom decoder. Following the documentation: https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#springencoder-configuration:~:text=Spring%20Cloud%20OpenFeign%20does%20not%20provide%20the%20following%20beans%20by%20default%20for%20feign%2C%20but%20still%20looks%20up%20beans%20of%20these%20types%20from%20the%20application%20context%20to%20create%20the%20feign%20client%3A, I have the following classes:

Added the config class

@FeignClient(
    value = "rateservice",
    url = "${rateservice.api.baseurl}",
    configuration = CustomFeignConfig.class)
public interface RateClient {
  @RequestMapping(method = RequestMethod.POST, value = "/calculatedrates")
  Rate calculateRate(RateRequest request);

  @RequestMapping(method = RequestMethod.POST, value = "/overrides")
  RateOverrideResponse createRateOverride(RateOverrideRequest rateOverrideRequest);
}

Config class

@RequiredArgsConstructor
@Configuration
public class CustomFeignConfig {

  private final CustomErrorDecoder customErrorDecoder;
  @Bean
  public ErrorDecoder errorDecoder(){
    return customErrorDecoder;
  }

Implementation of the custom error decoder
@Component
@Slf4j
public class CustomErrorDecoder implements ErrorDecoder {
  @Override
  public Exception decode(String methodKey, Response response) {
    log.info(response.toString()); // For now just making it hit the breakpoint
    return new RuntimeException();
  }
}

However, I see the customErrorDecoder not coming into effect.

I set a breakpoint in the error-decoder and it wasn't getting hit when I intentionally tried to fail the feign client call. Thanks you for the help in advance.

Adi
  • 23
  • 4
  • The way I was trying to fail the feign client call was by not switching on the server on the other side of the call which was wrong. Error decoder works when the http call itself is not successful and not when the call itself is not made in the first place – Adi Mar 18 '23 at 00:21

0 Answers0