Form API - match field with error
Drupal does it on elements theme level with _form_set_class function. This function gets $element array and checks if it has errors with function form_get_error. Because errors are set with indexes like 'foo][bar][value' (parents) that can be not so easy to find them in the $form array. So I suggest to use the same check as in _form_set_class with your function at pre_render, theme or validate level when you have $element array. hook_element_info_alter can be useful.  

The super simple example:

function YOUR_MODULE_element_info_alter(&amp;$elements) {
  foreach ($elements as $type =&gt; $value) {
    $elements[$type]['#pre_render'][] = 'YOUR_MODULE_element_error_check';
  }
}

function YOUR_MODULE_element_error_check($element) {
  if (isset($element['#parents']) &amp;&amp; form_get_error($element)) {
    $element['#suffix'] = t('error!');
  }
  return $element;
}