Drupal 7 – Debugging Illegal offset type in isset or empty in … and other errors

Though the title mentions Drupal 7, this article is really about the debugging technique and so is applicable to general php as well. I want to share a recent bug we had to solve at work. Though experienced developers would usually have an idea what new code is breaking, mysterious warnings and errors sometimes do slip through while working in teams (even when having a proper svn system in place!). You check with your co-developers and the one who broke it have no idea where this might be coming from, or worse: they have seen and have been ignoring this error for some time because they felt it didn’t matter. What do you do then?

Read the error
The error message “Illegal offset type in isset or empty in…” points to an illegal type used in a call to isset or empty, and it conveniently points you to the offending line – but with Drupal’s (and most of today’s frameworks’) complex page execution flows, it is hard to spot where is the real source of error, especially if the line is in one of the framework’s frequently called core routines (in my case it was in user_access). Since this is not much help in trying to deduce the real source, I resort to setting my own error handler (temporarily overriding Drupal’s) so that I can do a debug_back_trace().

To set your own error handler, define a function and feed the name to php’s set_error_handler():
[php]
// Your custom handler
function my_error_handler($errno, $errstr, $errfile, $errline
, array $errcontext)
{
// error was suppressed with the @-operator, ignore
if (0 === error_reporting()) {
return false;
}

// print the backtrace
echo ‘<pre>’.print_r(debug_backtrace(),true).'</pre>’;

// throw an error exception if you want to catch it at the caller
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

// Back at the offending line:
set_error_handler(‘my_error_handler’);

try {
// original code
}
catch (ErrorException $e) {
// print your own debug log or what not
}

// Restore Drupal’s own error handler. You don’t want a backtrace
// on every other E_USER_NOTICE’s do you?
restore_error_handler();

[/php]

While you may find it useful to convert every error into an exception, note that even E_USER_NOTICE would then halt your page execution, so please use with care (refer to php manual on the ErrorException class for a handler that ignores non-fatal errors).

Now, what’s left is to carefully read through the debug backtrace and spot the problem. For my case, it was a line of code someone embedded in a view context. Hope this helped, share your own debugging methods and opinions in the comments.

End note
Some people may feel that my style of debugging isn’t very ‘elegant’ but my experience is that the fastest way to find the problem isn’t necessarily the most elegant way. Rather than going through hours of deducing which functions might or might not be involved, and later realising the problem was in one of those functions that “logically shouldn’t have mattered in this case”, I find that getting a solid backtrace – after a quick deduction fails – is usually my best bet in the long run.

2 thoughts on “Drupal 7 – Debugging Illegal offset type in isset or empty in … and other errors

  1. Jose Manuel Velasco

    Hi, nice one.

    Btw, where might I place this function? I am quite new to drupal and I am having this kind of error creating a custom content type from code and I wonder why, hope your trick could help me but I can’t figure out where to write your function.

    Could you point me to the right direction?

    Thanks in advance.

    Regards,
    ยท_-

    Reply
    1. squall3d

      You can add your custom handler in any file that would be bootstrapped. Try appending it to the end of the .module file that’s giving you issues.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *