I am developing an Android Library which will be distributed to other developers. The library itself is a wrapper of the industry specific one. This makes the API development a bit less flexible. The main goal is to provide an easy and understandable API to the external developers.
For now I've selected an asynchronous approach as a basis for the public API. The simplified version of the public API look like this:
MyApi{
public void login(String user, String password, LoginListener listener){...}
public void doAction1(String actionParam, Action1Listener listener){...}
public void doAction2(String actionParam1, String actionParam2, Action2Listener listener){...}
}
All the names are selected just for this question.
So the listener (interface) in each method may have arbitrary number of methods. I try to limit the number as much as possible. One method that each of the listeners have is onError(XXXError)
.
So there is a separate type of Error for each type of listener. Errors extend RuntimeException
class an may have common traits. Such as "caused by network problem" error.
For example, this is how the LoginListener interface looks:
public interface LoginListener {
void onLoginSuccess();
void onLoginFailed(LoginError error);
}
As a result the user of my API may either display some default error message or do an additional check. Additional check may be required since the error may be caused by different communication problems (network connection, bad input params, internals server error, etc.) in case.
Here is the implementation:
LoginListener loginListener = new LoginListener(){
@Override
public void onLoginSuccess() {
//proceed
}
@Override
public void onLoginFailed(LoginError error) {
if(error.isNetworkError()){
//ask user to check network connection
}else{
//display default message
}
}
};
For Action1
and Action2
, the errors are more complicated and may involve even instanceof
comparison to get the exact reason.
Isn't error typing an overhead? What are good examples of error handling/notification?
An option of implementing one MyApiError
class with an enum of causes, sounds like a non-flexible and misleading one.