0

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.

2
  • 1
    You are logging it yourself, so why shouldn't it be printed... Commented Nov 14, 2024 at 18:48
  • If the error is printed then something wrong happens to the underlined code that needs to fix. If you don't know how to fix the problem, then you should make a step back to figure out why this exception occurred and how to prevent it. Maybe some more effort is required to distinguish the actual problem. Commented Nov 14, 2024 at 18:58

1 Answer 1

0

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); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.