1

I would like to create a custom error page in Drupal 7. There are things like set_message, but they don't log the errors. So is there any hook or something similar to catch the error, log it and display a human error to my users?

Michiel
  • 7,855
  • 16
  • 61
  • 113
  • found something similar here http://stackoverflow.com/questions/1705840/how-to-log-error-message-in-drupal – Vishal Khialani Dec 07 '11 at 15:43
  • let me know if it works out for you . If not then we can work on something better – Vishal Khialani Dec 07 '11 at 15:52
  • Your question is a little confusing. Are you saying you want to capture errors reported by 'set_message' and redirect users to a dedicated error page? If so, that would probably break a lot of default drupal functionality. – Coder1 Dec 07 '11 at 22:53

2 Answers2

1

You stated above that your goal is "catch the error, log it and display a human error to my users".

In that case you're probably looking for the functionality Try/Catch which allows you to try to run a block of code and if something goes wrong it will display a message.

In your particular case you can log the error to Drupal's database logging system with the watchdog function http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/watchdog/7

Optionally you could also log this to the PHP error_log as well see http://php.net/manual/en/function.error-log.php

Then you could continue on displaying the message to the user using the drupal_set_message function that you already figured out.

The final code for what you're trying to accomplish would look something like this:

try {

  // RUN YOUR CUSTOM CODE HERE

} catch (Exception $e) {

  // Record the error Drupal's database log
  watchdog('error_page', $e->getMessage());

  // Record the error to PHP's error_log
  error_log($e->getMessage());

  // Display a message to the user
  drupal_set_message("We're sorry, but we couldn't find the page you were looking for.", 'error');
}
Joshua Powell
  • 894
  • 1
  • 9
  • 13
0

drupal_get_messages() could be used to fetch an array to iterate through for the the error types of messages.

I'm not sure I made 100% sense of your question, but since you referenced drupal_set_message() i thought this might be what you were looking for.

You could handle it in hook_init(), check for messages there, if you find any, do something with it.

Redirecting on an error though could potentially break default drupal functionality like forms.

Coder1
  • 13,139
  • 15
  • 59
  • 89