[英]Error message always "null" from RestTemplate response
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.
我有2台服务器,一台是后台的微服务,一台是调用它的主服务器。
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);
从微服务内部我只是
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() );
).出于某种原因,在我的主服务记录器中,我得到了“503 null”的日志,就像这是抛出的错误中的消息(来自行
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.但是,如果我直接向我的微服务发送 postman 请求,我会收到完整的错误消息,其中包含正确的消息。
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?
如何传递正确的错误消息以传播到主要服务?
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) {
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.