views:

23

answers:

1

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?

+2  A: 

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.

Reed Copsey
I'm glad you mentioned exceptions in non-exceptional circumstances as that my chain of thought.I'm not sure I follow you on the use of validation in the API however. Are you suggesting that the API would expose a validate method that would return some token to be used in the subsequent call to retrieve the actual response data?
mmccomb
@mmccomb: That would be one option. Another would be to pass a class that contained the info required for validation, and pass a reference to it into each method as required, etc.
Reed Copsey
@mmcomb: I'd strongly suggest not using exceptions as flow control, though - which was kind of what you were suggesting...
Reed Copsey