Forum OpenACS Development: Unexpected error returned from call to template::element::get_value

Hmmm

So I'm using template::element::get_value form_id element_id and it's code implies (though docs don't clearly state) it would return an empty string if the element doesn't exist.

get_reference

  if { [info exists element(value)] } {
    return $element(value)
  } else {
    return ""
  }

And code that calls it from Classified Ads, clearly believes this to be the case.

set title [template::element::get_value $form_id $title_column]
        if {[empty_string_p $title]} {
            # make sure there is always a title
            set title $name
        }               

However it throws an error instead. So when I call it and the element doesn't exist I get an error thrown instead of a nice blank string returned.

The culprit seems to be in the Templating system code:

ad_proc -private template::element::get_reference {} {
    Gets a reference to the array containing the properties of an
    element, and throws and error if the element does not exist.  Called
    at the beginning of several of the element commands.
} {
  uplevel {

    set level [template::adp_level]
    upvar #$level $form_id:$element_id element

    if { ! [array exists element] } {
      error "Element \"$element_id\" does not exist in form \"$form_id\""
    }
  }
}

As the error is:

Element "title" does not exist in form "edit_ad"
    while executing
"error "Element \"$element_id\" does not exist in form \"$form_id\"""
    invoked from within
"if { ! [array exists element] } {
      error "Element \"$element_id\" does not exist in form \"$form_id\""
    }"
    ("uplevel" body line 6)
    invoked from within
"uplevel {

    set level [template::adp_level]
    upvar #$level $form_id:$element_id element

    if { ! [array exists element] } {
      error "Elem..."
    (procedure "get_reference" line 2)
    invoked from within
"get_reference"
    (procedure "template::element::get_value" line 2)
    invoked from within
"template::element::get_value $form_id $title_column"
    (procedure "cms-forms::new_cr_item" line 30)
    invoked from within
"cms-forms::new_cr_item -item_id_element item_id  -form_id $form_name -content_type $content_type"
    invoked from within
"if { [template::form::is_valid $form_name] } {

I don't know if code in other packages relies on an error being thrown from template::element::get_reference. So is the "right" way to fix this, to wrap the call to template::element::get_reference with a catch in template::element::get_value? Assuming no other calls to template::element::get_value are relying on the error being thrown. The docs for template::element::get_value don't clearly state the expected return on non-existence of a value for the element being sought. Though the code clearly looks like it intends to return an empty string (see above posted code).

How to proceed to keep this code most reusable please?!
thanks:)

Tammy, I would vote for catching the error and returning the "implied" empty string as the return value.

I have noticed many times while searching the API docs that programmers seldom explicitly document a proc's return value. To me this is sloppy and leads to many subtle runtime errors.

I would love to see each and every proc documented with a @return property...

I don't know what's the right way to propose to get something like this into the OACS release code. Maybe it's to submit it as a bug? Anyone have experience with the process?

I agree that it would it clean up some subtle runtime errors in this case fer sure.

thanks.