OpenAPITools / OpenAPITools/openapi-generator
[JAVA][Question] How to cast ApiException to UnknownHostException in Java?
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
I am able to generate a Java client and use it in my Android app like this:
MyAPI apiInstance = new MyAPI(defaultClient);
String param1 = "param1_example"; // String |
String param2 = "param2_example"; // String |
try {
MyResponse result = apiInstance.apiMethod(param1, param2);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling MyAPI#apiMethod");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
System.err.println("Response message: " + e.getMessage());
e.printStackTrace();
}
The issue I am having is that I want to be able to catch the IOException if the Android phone does not have an internet connection or if the connection times out. For no internet connection, the exception is UnknownHostException. I tried to cast the ApiException to UnknownHostException but it the compiler won't let me because ApiException extends regular old Exception and thus cannot be cast otherwise.
But what I do know is that when I have no internet connection and catch the APIException and print out e.getMessage() I get this: Message: java.net.UnknownHostException: Unable to resolve host "apihost.com": No address associated with hostname.
So, the ApiException must somehow know this is an UnknownHostException because it prints it in the message.
Does anyone know how I might catch the UnknownHostException or other IOExceptions like ConnectException when calling the API? Can I get it out of the APIException somehow? (preferably without parsing the string message)
Or is there another way to detect if the connection disconnected or timed out? Checking HTTP status codes won't do any good here, because the request never goes through.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The report names no repository file or test; start with the generated Java client's ApiException and defaultClient call path used by MyAPI#apiMethod. Trace how UnknownHostException and other IOExceptions are represented, then confirm whether callers can distinguish network failures without parsing messages; done means the supported behavior is documented or covered by a focused test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java
- Domain
- api, mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100