By Peter Bell

How do YOU Handle Errors in your beans and model?

I have a Product IBO (lets call it a bean for the purposes of this discussion). I have loaded it with unverified values from the form scope (which I don't want to error - I want to be able to put a text string into a date field without CF falling over - one of the reasons I like dynamic typing).

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!

Comments
DISCLAIMER: I have practically no experience with type of programming.

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.
# Posted By Ben Nadel | 10/17/06 12:33 PM
I do something similar to Ben, except my beans actually do a cfthrow type="validation" when the data is no good. The controller catches those errors and adds them to an array of errors in the request scope. My framework (homegrown) looks for errors in this array and outputs them at the top of the page.

Any true exceptions (like database unavailable, syntax errors, etc.) I just let Application.cfc onError() handle.
# Posted By Rich Thibault | 10/17/06 1:56 PM
For example, if I have a file with a registration form for a user I would have an if first to test if the form is being submitted. After that I have a base function called validate() that determines whether all the properties in the user object are valid or not. It returns a boolean value.

<cfif variables.user.validate()>
<cfset variables.user.save()>
<cfelse>
<cfset variables.hasErrors = true>
</cfif>
# Posted By Javier Julio | 10/17/06 11:18 PM
Peter,

It's necessary to handle errors if arguments of method are required? It's not the same job to capture with the cfcatch?

Jeff
# Posted By Jeff | 4/1/07 5:51 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.