I sent this question to the Android development team, but I wanted to post it here for other people who might encounter design issues like me:
I was looking for an idiom or paradigm on how to handle HTTP errors.
Basically,
I have an AsyncTask that runs in a background thread that calls the static method my callRequest ().
This is all done in the doInBackground () part. There are two types of exceptions that executeRequest () throws. IOException for all communication errors and ServerErrorResponse exceptions, which is my own exception. This can happen if, for example, the client sent something bad to the server, all HTTP worked, but the server complained (maybe I passed an invalid parameter or id).
So, what I did is the result of the result in the "result object".
In onPostExecute (), I check if the result was unsuccessful, and then I try to process it in the user interface thread. However, now I have to start doing
Exception e = result.getException(); if (e != null) { if (e instanceof IOException) { //network error //handle network error here } else if (e instanceof ServerErrorResponseException) { //handle server error response here }
As you can see, this is getting annoying. For each new exception, I have to check it with instanceof. Is there a way around this or a design that I could follow to avoid this? I want exceptions to be handled in the user interface thread in case I show a dialog box or something to the user.
Any ideas?
source share