Hide
on 31/3/07

Have you ever wondered what the reason for that 404 error is when you set debug to 0, or when you move your site onto your live server? How many visitors/clients are confronted by a 404 when they should be looking at the fruits of your work? Cake allows you to modify what happens in the case of an error, and the purpose of this post is to show how to make your site mail you (or the website admin if it's not you) or take whatever other action you might want whenever a 404 is generated.

I'll keep it short and sweet today :). Create the file app/app_error.php app/error.php with the contents below:

  1. <?php
  2. uses('error');
  3. /**
  4. * AppError class. Used to customize the way errors are handled.
  5. *
  6. * This class allows the application to modify the behavior in the case of an error.
  7. */
  8. class AppError extends ErrorHandler {
  9. /**
  10. * Constructor method. Overriden to email the site admin if a 404 is generated, with the real reason for the error
  11. * message.
  12. *
  13. * @param type $name description
  14. * @return type description
  15. */
  16. function __construct($method, $messages) {
  17. if (!DEBUG) {
  18. $logItAswell = true;
  19. $message = 'Site '.env('SERVER_NAME').' has generated an error message'."\n\n";
  20. $message .= "Error Type: $method\n";
  21. foreach ($messages[0] as $key => $value) {
  22. $message .= str_pad($key, 10).": $value\n";
  23. }
  24. $message .= 'Referrer : '.env('HTTP_REFERER')."\n";
  25. $message .= 'Remote add: '.env('REMOTE_ADDR')."\n";
  26. @mail(env('SERVER_ADMIN'), env('SERVER_NAME').' Error: '.$method, $message);
  27. if ($logItAswell) {
  28. $message .= 'Session : '.serialize($_SESSION)."\n";
  29. $message .= 'Session : '.($_POST)."\n";
  30. $this->log($message);
  31. }
  32. }
  33. parent::__construct($method,$messages);
  34. }
  35. }
  36. ?>

Include this file anywhere in your code, bootstrap is as good a place as any, with include(APP.'app_error.php');. and that's it. Whenever your site generates an error you get an email with the basics, and more details in the log file. A related bit of info is available over on Cakebaker's blog, which is where I realized that if you name the file 'error.php' it gets included automatically.

Bake on!

2 Responses to Email me my site errors

  1. 1

    I have a similar thing for one of my sites.

    Just that it goes one step further and mails an error only once - thus preventing a flooding attack.

  2. 2

    Hi Tarique,

    I wanted to keep the article as simple as possible, and in some ways a flood of emails is a good thing to give urgency to atleast identify the problem :).

    I did this with an online shop I inherited before and was appalled by the number of emails I received from the order processing page. What a surprise, most errors were not followed by an order by the same user...

    But back to the here and now, even for a medium-traffic site, this may not be suitable in an unmodified form; although it depends in part whether there is a catch all route and /asdf doesn't trigger a missing* error. To reduce the potential number of errors logged/emails sent it could be modified relatively easily to ignore missing controller/action errors that don't have a local referrer (i.e. didn't come from a link on the site), and let through all other high priority (missingComponent missingHelper, noConnection etc. ) errors untouched.

    How did you keep track of duplicates btw? checking strpos of the url/error in the log file before sending the email? unique files for each error?