0

I'm losing my mind over here. I have 2 servers, one is micro-service behind the scenes and one is the main server that calls it.

In the micro service, I throw an exception that is defined like this:

@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
public class CGTokenException extends Exception{

   // private String msg;

   public CGTokenException() { super("Unknown error getting token / Validating card!"); }

   public CGTokenException(String msg) {super(msg); }
}

In my main server, I use it like this:

   @Override
    public
    GetPaymentTokenResponse getToken(GetPaymentTokenRequest getPaymentTokenRequest, String url) {

        try {
           String fullPath = url + "/Token/getPaymentToken";
            return this.restTemplate.postForObject(fullPath, getPaymentTokenRequest , GetPaymentTokenResponse.class);
        }
        catch (Exception ex) {
            log.error("Error getting TOKEN / validating card from Credit Guard");
            log.error( "CGTokenController error: " + ex.getMessage() );

            ex.printStackTrace();
        }
}

From inside the microservice I simply do throw new CGTOkenException(msg);

For some reason tho, in my main-service logger I get the log of "503 null", like this is the message inside the thrown error (from the line log.error( "CGTokenController error: " + ex.getMessage() );).

But if I send a postman request directly to my microservice- I get the full error with the correct message inside.

what am I doing wrong? I guess the problem is how I catch it in my main service... but I just can't find what it is. how can I deliver the correct error message to propagate to the main service?

Gibor
  • 1,695
  • 6
  • 20

1 Answers1

0

You either need to catch the right exception or cast it.

try {
 String fullPath = url + "/Token/getPaymentToken";
 return this.restTemplate.postForObject(fullPath, getPaymentTokenRequest , GetPaymentTokenResponse.class);
}
catch(HttpClientErrorException hcee) {
  if (hcee.getRawStatusCode() == 503) {
    ...
  }
}
codebrane
  • 4,290
  • 2
  • 18
  • 27