I’m currently writing an app that posts some data to a HTML form and parses the resulting HTML response page. Depending on the input my user sends the HTML response can either contain valid payload data or request that the user validate an error in the data they entered.
I’m slightly confused as to how to implement the interface for my call to the HTML service. Specifically do I use exceptions to handle the responses which require further user validation or do I incorporate them into a hierarchy and use the base type as my interface return type?
Is there a best approach for implementing such interfaces?
It really depends on how you see this being used.
I would not build an API that used Exceptions for “normal” actions. If, in your API, it is not obvious whether validation will be required in advance, I think that building the checks directly into the API, along with a clean way to verify this in advance would be preferable to exceptions.
If, however, it’s fairly obvious in advance whether or not the validation is required, I’d try to build it into the API (ie: make the method calls require the validation information to be passed in). This would prevent the need for the exceptions in most cases. You can then raise an exception when necessary.
In general, I strongly recommend thinking of exceptions as “exceptional”, not a mechanism for flow control. A user shouldn’t have to handle exceptions in order to use your API effectively.