I have a large number of complicated views on a site, some of which, when searched with certain options or by certain users, give the following error:

An illegal choice has been detected. Please contact the site administrator.

Is it possible to somehow catch this error and email the admin? Ideally, I would be able to include the URL of the view causing the problem and the UID of the user accessing the view for troubleshooting purposes.


You are describing hook_watchdog(array $log_entry):

This hook allows modules to route log events to custom destinations, such as SMS, Email, pager, syslog, ...etc.

emphasis mine.

Inside it, you can use $log_entry array to get UID and request URL, and you can also use global $user, and you can call current_path(), so both your requirements seems doable.

$log_entry array is defined as:

$log_entry: An associative array containing the following keys:

  • type: The type of message for this entry.
  • user: The user object for the user who was logged in when the event happened.
  • uid: The user ID for the user who was logged in when the event happened.
  • request_uri: The request URI for the page the event happened in.
  • referer: The page that referred the user to the page where the event occurred.
  • ip: The IP address where the request for the page came from.
  • timestamp: The UNIX timestamp of the date/time the event occurred.
  • severity: The severity of the message; one of the following values as defined in RFC 3164:

    • WATCHDOG_EMERGENCY: Emergency, system is unusable.
    • WATCHDOG_ALERT: Alert, action must be taken immediately.
    • WATCHDOG_CRITICAL: Critical conditions.
    • WATCHDOG_ERROR: Error conditions.
    • WATCHDOG_WARNING: Warning conditions.
    • WATCHDOG_NOTICE: Normal but significant conditions.
    • WATCHDOG_INFO: Informational messages.
    • WATCHDOG_DEBUG: Debug-level messages.
  • link: An optional link provided by the module that called the watchdog() function.

  • message: The text of the message to be logged. Variables in the message are indicated by using placeholder strings alongside the variables argument to declare the value of the placeholders. See t() for documentation on how the message and variable parameters interact.
  • variables: An array of variables to be inserted into the message on display. Will be NULL or missing if a message is already translated or if the message is not possible to translate.

There is no way I know to get form ID of a form that created your error, I don't think it is in ['variables'], so the best you can do is to parse ['message'] for "illegal choice", and test ['request_uri'] and ['referer'] for pages that are your suspects.

  • Thanks. It looks like there's a lot of different possible causes for this error message. Is there an efficient way to isolate causes of this specific error message? Sep 26 '14 at 11:51
  • @PatrickKenny answer updated
    – Mołot
    Sep 26 '14 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.