How do YOU Handle Errors in your beans and model?
Now I am ready to validate the input and save the product. Lets use an AR style Product.Save() which (for me) orchestrates transformations, validations and (if valid) the save. How do you report on the validity of the data?
Do you keep it in the bean with an if Product.IsValid() Product.Save() or do you do an Error = Product.Validate() - if NOT Error Product.save()?
I'm guessing I know what most people do with the above case (although I'll be interested in feedback), but what about other kinds of errors? Lets say you have a db connection error - how does that get reported? Do you have a Product.saved() which confirms whether the save worked? Do you do a Error = Product.Save() and explicitly return an error object?
Any ideas appreciated as I'm trying to refine my rather ropy error handling within the model.
Also, does anyone pass around an Error object or even an ErrorStack object for catching the n-errors that could bubble up. If so, what kind of methods and/or properties does it have?
Any advice, experience or wild asses guesses much appreciated!



When I was first exploring stuff like this I had a collection object: ContraindicationCollection. This collection held reasons that were contraindicators of why the data should NOT be committed to the database (ie. a certain value was not supplied or was not valid). Each contraindication had the internal variable name that errored (ie. DateCreated), the form field (ie. date_created), the current value (ie. "#VARIABLES.DateCreated#") and a message "Date Created is not a valid date".
Then, it was the controller's job to see if there were contraindications:
if ( Contraindications.Size() ){
// Do NOT save data
} else {
// Save data
// ex. Bean.Save()
}
This may seem like a wierd way of doing it as a I create a Bean that can save itself, but does know IF IT SHOULD. This way, if i had a page that had several beans (ie. contact and address bean), I could gather and union contraindicators from all beans, then decide if I wanted to proceed.
This might be crazy, but it worked for my project... but, since then I have gone away from this as it was stressing me out too much.
Any true exceptions (like database unavailable, syntax errors, etc.) I just let Application.cfc onError() handle.
<cfif variables.user.validate()>
<cfset variables.user.save()>
<cfelse>
<cfset variables.hasErrors = true>
</cfif>
It's necessary to handle errors if arguments of method are required? It's not the same job to capture with the cfcatch?
Jeff