User login

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 at http://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 at http://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

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.