简体   繁体   English

错误没有从被叫服务传播到呼叫者服务

[英]Error not getting propagated from called service to caller service

We have 2 services which have REST controllers. 我们有2个具有REST控制器的服务。

Caller Service calls like: 呼叫者服务呼叫如下:

try {
            response = restTemplate.exchange(urlModifyAttributes, HttpMethod.PUT, new HttpEntity<>(attributesMap, headerMap), parameterizedTypeReference, uriVars);
            return response.getBody();
        } catch (RestClientResponseException e) {
            log.error("Modify attributes failed. Error {}", e.getMessage(), e);
            throw new RuntimeException(e.getMessage());
        } catch (Exception e) {
            log.error("Modify attributes failed. Error: {}", e.getMessage(), e);
            throw new RuntimeException(e.getMessage());
        }

While the called service throws a RuntimeException like: 当被调用的服务抛出RuntimeException时,如下所示:

throw new RuntimeException("Already modified");

But the caller service is not able to capture the error message "Already modified" using e.getMessage() . 但是,调用者服务无法使用e.getMessage()捕获错误消息“已修改”。 Rather e.getMessage() is returning something like "I/O error on PUT request for http....... " Is there a way by which I can capture the error message at the caller service level? 而是e.getMessage()返回类似“在对HTTP的PUT请求上发生I / O错误……”的方法,我可以在调用者服务级别捕获错误消息吗?

Both services are SpringBoot Applications and have RestControllers 这两个服务都是SpringBoot应用程序,并具有RestControllers

You are handling exception in caller service but in called service it need to send the exception as well. 您正在呼叫者服务中处理异常,但在被叫服务中,它也需要发送异常。 For generic u can refer this or have your own custom impl. 对于泛型,您可以引用此名称或具有自己的自定义隐式。

called service: 被叫服务:

@Getmapping("/uri")
public ResponseEntity<Object> getIt(@RequestBody MyRequest req) {
try{
//...business logic

    return new ResponseEntity<>(response, HttpStatus.OK);

}catch(MyException e){
    return new ResponseEntity<>(e, HttpStatus.SOME_EXC_STATUS); //or custom exc
}catch(Exception e){
    return new ResponseEntity<>(e, HttpStatus.SOME_EXC_STATUS);
}
}

Then only you will be able to get exception status. 这样,只有您才能获得异常状态。

The called service needs to send this in the response. 被调用的服务需要在响应中发送它。 You are making a rest call. 您正在打个电话。 Had it been a method, you could have caught it from caller too. 如果这是一种方法,那么您也可能从调用方那里捕获了它​​。

In the response entity of the called service pass the exception message. 在被叫服务的响应实体中传递异常消息。 Either using controller advice or responsestatusexception. 使用控制器建议或responsestatusexception。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM