'How to get original error thrown by another API?

Forgive me, if something I say is not totally correct, I am learning.

I have a web application A generated with JHipster. It is the main one that the end users will use. I have a second web application B that has a its own database, and frontend with what you can manipulate the data in the database. Application A can request data from B through a gateway, which then requests data from B and maybe some others, formats the data and sends it back to A.

If I were to raise a BadRequestAlertException in A backend, the ExceptionTranslator that JHipster created will catch it and use the errorKey parameter to translate it. But if A requests data from B and B raises a BadRequestAlertException, the ExceptionTranslator in B catches the error, but in A I get a HttpClientErrorException. I would like to get the original error that was thrown, so that ExceptionTranslator of A could catch it and translate the error as I want to show an error message to the user. I could read the errorKey from the response body of HttpClientErrorException using getResponseBodyAsString() and throw BadRequestAlertException again, but all this doesn't seem right, because I have to start parsing the error string. How can get the original error or is there a better way to do this kind of thing?



Solution 1:[1]

What you're asking is not possible.
Service A and B are two different services, so you can't get the actual exception from B inside A - all you can is get the HTTP response B returns (A translates it to HttpClientErrorException but this exception is generated inside A, not B).

You don't have to generate an exception out of it, you can simply take the body and status you got from B and return it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nir Levy