Why custom Rest Error Handler is not getting called when rest template throw exception?
Asked Answered
S

2

6

I am trying to cover all of the rest template calls in my class for exception handling. Using Custom exception handling with error handler in spring boot application.

For this i have created a rest template bean in config and set error handler in it to the custom error handler class that i created with extends DefaultResponseErrorHandler.

public class BaseConfig {
@Bean
    @Primary
    RestTemplate restTemplate(@Autowired RestTemplateBuilder restTemplateBuilder) {
        return restTemplateBuilder.errorHandler(new IPSRestErrorHandler()).build();
    }
}
@Component
public class IPSRestErrorHandler extends DefaultResponseErrorHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(IPSRestErrorHandler.class);

    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        if (response.getStatusCode()
                .series() == HttpStatus.Series.SERVER_ERROR) {
            LOGGER.error("Server error with exception code  : "+response.getStatusCode()+" with message : "+response.getStatusText());
            throw ExceptionUtils.newRunTimeException("Server error with exception code  : "+response.getStatusCode()+" with message : "+response.getStatusText());
        } else if (response.getStatusCode()
                .series() == HttpStatus.Series.CLIENT_ERROR) {
            LOGGER.error("Client error with exception code  : "+response.getStatusCode()+" with message : "+response.getStatusText());
            throw ExceptionUtils.newRunTimeException("Client error with exception code  : "+response.getStatusCode()+" with message : "+response.getStatusText());
        } else {
            LOGGER.error("Unknown HttpStatusCode with exception code  : "+response.getStatusCode()+" with message : "+response.getStatusText());
            throw ExceptionUtils.newRunTimeException("Unknown HttpStatusCode with exception code  : "+response.getStatusCode()+" with message :"+response.getStatusText());
        }
    }
}
public class ServicingPlatformSteps {

 @Autowired
    private RestTemplate restTemplate;

 private ResponseEntity callServicingPlatformAPI(RemittanceV2Input inputClass) {
ResponseEntity entity = null;
entity = restTemplate.exchange(builder.build().encode().toUri(),
                    org.springframework.http.HttpMethod.POST, httpEntity, typeRef);
return entity;
}

Here i am expecting that when restTemplate.exchange method is called and it throws some exception then my IPSRestErrorHandler should be called. But error handler is not getting called. While i am getting this restTemplate instance with error handler info in it.

Could you please help me why error handler is not getting called?

Strader answered 12/4, 2019 at 12:54 Comment(0)
A
0

In your case replace below

@Component
public class IPSRestErrorHandler extends DefaultResponseErrorHandler {

}

with

@Component
public class IPSRestErrorHandler extends ResponseErrorHandler {

}

Please see, ResponseErrorHandler will ensure that the HTTP status from the response is read. So we have to extend the same.

And you already have injected the IPSRestErrorHandler implementation into the RestTemplate instance.

You can read more here , it explains how you can unit test also.

Hope it helps.

Americano answered 14/4, 2019 at 15:25 Comment(0)
W
0

You should annotate BaseConfig with Contiguration. Also, see if you have another config class prividing Bean of RestTemplate.

Waldrop answered 1/7, 2021 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.