简体   繁体   中英

Error not getting propagated from called service to caller service

We have 2 services which have REST controllers.

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:

throw new RuntimeException("Already modified");

But the caller service is not able to capture the error message "Already modified" using 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?

Both services are SpringBoot Applications and have 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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