MxS
MxS

Reputation: 1

Spring Boot with Rest Template, error catching errors

I stopped an instance I call using rest template to be able to catch the errors thrown. The errors are:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for

Caused by: java.net.SocketTimeoutException: Connect timed out

I handle the errors in try / catch block, and I log to verify if the ResourceAccessException is caught.

The log was printed, so the error was caught, and return a default value inside that catch block.

But the problem is that the error is still printed in the spring console, and I can't stop it.

try {
    ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);

    return PipsUtils.roundToTwo(Float.parseFloat(Objects.requireNonNull(exchange.getBody())));
} catch (ResourceAccessException e) {
    log.error("An error occurred while connecting to instance with port: {}", port, e);
    throw new InstancesException("Connection to instances refused!");
}

I want to catch the error and stop from printing it in the console.

Upvotes: 0

Views: 140

Answers (1)

Anubhav Sharma
Anubhav Sharma

Reputation: 543

I know I shouldn't do this. Anyways, try below code.

try {
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        return PipsUtils.roundToTwo(Float.parseFloat(Objects.requireNonNull(exchange.getBody())));
} catch (ResourceAccessException e) {
       // I don't know return type of PipsUtils.roundToTwo
        return PipsUtils.roundToTwo(0f); 
}

Upvotes: 0

Related Questions