Finding run-time Drupal errors in Emacs
Earlier I stumbled on a nifty compile-mode trick for emacs, and I used it to quickly jump to errors reported by coder.module. Here's another useful one, to quickly find errors and warnings at run-time.
Drupal reports PHP errors, warnings and notices to the watchdog and/or to the page. So as you're coding a new module you'll see something like this, just to pick one I recently noticed :
Warning: Invalid argument supplied for foreach() in pay_form->form_validate() (line 368 of /path/to/drupal/profiles/custom/modules/pay/includes/handlers/pay_form.inc).
This message comes from PHP, as displayed by Drupal's error handler. It's nice that it identifies the exact line of the problem. But it's not ideal that you then have to then look up that location manually. I'm always forgetting the line number by the time I open the file.
So, here's how I'm now jumping to these errors a little more easily...
First, we need to get those error messages in a place where emacs can find them. For this, I'm using syslog_ng.module. Drupal core's syslog.module should work as well, it just happens that I'm using syslog-ng on my development machine. Following the instructions for that module, I got Drupal to report watchdog messages to /var/log/drupal/watchdog.log. And, I added my user to the "log" group to get around permission problems.
Now, these errors and warnings are written to a file. It's fairly simple to get emacs to understand them using M-x compile
. You don't need to be editing the file with the error in it, or even a file in that instance of drupal, although I typically am. My emacs doesn't know how to compile drupal files, so after M-x compile
it prompts me for a compile command. Here type:
tail /var/log/drupal/watchdog.log
This will get the most recent watchdog messages into the *compilation* buffer.
All that's left is to get Emacs to understand the format of the errors as presented. For this I added another regexp to compilation-error-regexp-alist
, by adding this to my .emacs:
(add-to-list<br> 'compilation-error-regexp-alist<br> ;; drupal log by syslog-ng.module <br> '("drupal: .*||\\(.+\\) \(line \\([0-9]+\\) of \\(.+\\)\)\." 3 2))
Evaluate that lisp, or restart emacs. Then repeat the M-x compile
.
Now you should see the line numbers and file names highlighted in the *compilation* buffer. And each press of C-x `
will bring you to the next error.
Very convenient!
Tags: