I have 2 APIs from 2 different companies that allow me to communicate with their servers in order to process transactions. I am tasked with creating a generic interface to these APIs. I came up with something like this:
IServiceProvider <- ServiceProvider <- CompanyAServiceProvider
IServiceProvider <- ServiceProvider <- CompanyBServiceProvider
In CompanyAServiceProvider
I am using the API that they offer to interface with their remote servers. This API from company A throws exceptions that are completely different from Company B's.
I can handle the exception locally but I don't really think that suites the situation.
public String purchase(String amount) {
try {
request = new Request( RequestIF.NEW_ORDER_TRANSACTION );
} catch ( InitializationException e ) {
//do something.
}
}
Or I can throw this exception to the caller:
public String purchase(String amount) throws Exception {
request = new Request( RequestIF.NEW_ORDER_TRANSACTION );
}
And let the caller handle just the Exception
no matter what that exception is and no matter what API throws it.
How can I write an interface to 2 different APIs and keep it generic when I am dealing with 2 different sets of thrown exceptions. Am I dealing with this correctly? What would be the best choice?