Throwing Errors in Drupal: Agaric's complete guide
return drupal_set_message("Chutzpah and wishful thinking ahead.", 'error');
All right, Agaric hopes to write a guide to not just displaying error messages but doing it all an a way sensible to a programmer.
Questions:
- How do you show a database SQL statement exactly in the form it ran to result in the error? I know I've seen these on my screen... how did I do it?!
There is no drupal "set_error" in Drupal 5: There is drupal_set_message
and the second parameter can be error.
The Drupal Pro Development book says:
Note that when you report errors, you should tell three things: what you were trying to do, why you can't do it, and additional information to which you have access. Often a friendlier error is displayed using
drupal_set_message()
to notify the user, and a more detailed error is written to the watchdog and is viewable athttp://example.com/?q=admin/logs/watchdog
.
What John VanDyk and Matt Westgate don't say is how to send separate errors to the log.
There's a great model from taxonomy.module, though:
/**
* Accept the form submission for a taxonomy term and save the result.
*/
function taxonomy_form_term_submit($form_id, $form_values) {
switch (taxonomy_save_term($form_values)) {
case SAVED_NEW:
drupal_set_message(t('Created new term %term.', array('%term' => $form_values['name'])));
watchdog('taxonomy', t('Created new term %term.', array('%term' => $form_values['name'])), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/term/'. $form_values['tid']));
break;
case SAVED_UPDATED:
drupal_set_message(t('Updated term %term.', array('%term' => $form_values['name'])));
watchdog('taxonomy', t('Updated term %term.', array('%term' => $form_values['name'])), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/term/'. $form_values['tid']));
break;
}
return 'admin/content/taxonomy';
}
return drupal_set_message("Chutzpah and wishful thinking ahead.", 'error');
All right, Agaric hopes to write a guide to not just displaying error messages but doing it all an a way sensible to a programmer.
Questions:
- How do you show a database SQL statement exactly in the form it ran to result in the error? I know I've seen these on my screen... how did I do it?!
There is no drupal "set_error" in Drupal 5: There is drupal_set_message
and the second parameter can be error.
The Drupal Pro Development book says:
Note that when you report errors, you should tell three things: what you were trying to do, why you can't do it, and additional information to which you have access. Often a friendlier error is displayed using
drupal_set_message()
to notify the user, and a more detailed error is written to the watchdog and is viewable athttp://example.com/?q=admin/logs/watchdog
.
What John VanDyk and Matt Westgate don't say is how to send separate errors to the log.
There's a great model from taxonomy.module, though:
/**
* Accept the form submission for a taxonomy term and save the result.
*/
function taxonomy_form_term_submit($form_id, $form_values) {
switch (taxonomy_save_term($form_values)) {
case SAVED_NEW:
drupal_set_message(t('Created new term %term.', array('%term' => $form_values['name'])));
watchdog('taxonomy', t('Created new term %term.', array('%term' => $form_values['name'])), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/term/'. $form_values['tid']));
break;
case SAVED_UPDATED:
drupal_set_message(t('Updated term %term.', array('%term' => $form_values['name'])));
watchdog('taxonomy', t('Updated term %term.', array('%term' => $form_values['name'])), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/term/'. $form_values['tid']));
break;
}
return 'admin/content/taxonomy';
}
Comments
Post new comment