PHPEdit.net Community
   
1 2 3 4
Tutorials Tips Pasties Code Snippets
 

Home > Code snippets > PHP > Nice error handleing for dev and live

Nice error handleing for dev and live

Created by Keloran, last update on 30/10/2008 11:38

A nice way to display errors when your on your dev area, and then for the same errors to be sent to your email address when the site is on live, without having to change the code all you have todo is, set your dev, live..defenitions e.g. if (strstr($_SERVER['HTTP_HOST'], ".dev") { define("DEV", true); } else { define("LIVE", true); }   and then include the extension handler afterwards

  1. <?php
  2. /**
  3. * The root of all errors.
  4. */
  5. class Error extends Exception {
  6. /**
  7. * Create a new error
  8. *
  9. * @param string $message the error message
  10. * @param int $errno one of E_ERROR, E_WARNING, etc.
  11. * @param string $file the path to the file in which the error
    occurred
  12. * @param int $line the line number in the file at which the error
    occurred
  13. */
  14. public function __construct($message = null, $errno = null, $file
    = null, $line = null) {
  15. parent::__construct($message, $errno);
  16. if ($file !== null) { $this->file = $file; }
  17. if ($line !== null) { $this->line = $line; }
  18.  
  19. if (defined("DEV")) {
  20. echo $this->showMessage();
  21. die();
  22. }
  23.  
  24. if (defined("LIVE")) {
  25. $this->sendMessage();
  26. $this->showNiceMessage();
  27. }
  28. }
  29.  
  30. /**
  31. * Modify the error message.
  32. *
  33. * @param string $message
  34. */
  35. public function setMessage($message) {
  36. $this->message = $message;
  37. }
  38.  
  39. /**
  40. * Send a message to me
  41. */
  42. function sendMessage() {
  43. $cMessage = str_replace(array("<p>", "</p>", "<br />"),
    array("", "\n\n", "\n"), $this->showMessage());
  44.  
  45. $cFrom = "From: " . $_SERVER['HTTP_HOST'];
  46. $myEmail = "";
  47.  
  48. $cTitle = "An error occoured in " .
    $_SERVER['HTTP_HOST'];
  49. mail($myEmail, $cTitle, $cMessage, $cFrom);
  50. }
  51.  
  52. /**
  53. * Show a nice message to users
  54. */
  55. function showNiceMessage() {
  56. $cMessage = "<p>A problem happend, please try again in '5
    Minutes'</p>"
    ;
  57. $cMessage .= "<p>Site Admin has been informed</p>";
  58. $cMessage .= "<p><a href=\"history.go(-1)\">Back</a></p>";
  59.  
  60. echo $cMessage;
  61. die();
  62. }
  63.  
  64. /**
  65. * Show the message
  66. */
  67. function showMessage() {
  68. $cMessage = "<p>An exception happened in <br />" .
    $this->getFile() . ".</p>";
  69. $cMessage .= "<p>On line <br />" . $this->getLine() .
    ".</p>";
  70. $cMessage .= "<p>The whole message is <br />" .
    $this->getMessage() . "</p>";
  71. $cMessage .= "<p>The exception code is <br />" .
    $this->getCode() . "</p>";
  72. $cMessage .= "<p>A Trace is <br />" .
    $this->getTraceAsString() . "</p>";
  73. $cMessage .= "<p>A Printed trace is <br />" .
    nl2br(print_r($this->getTrace(), 1)) . "</p>";
  74. $cMessage .= "<p>It was triggered by " .
    $_SERVER['REMOTE_ADDR'] . "</p>";
  75.  
  76. return $cMessage;
  77. }
  78. }
  79.  

Dependencies

No special requirements are needed by this snippet

By logging in you will be able to:

  • Recommand this page to someone else
  • Monitor changes on this item
  • Rate this item
  • Post comments
  • Download this item
Login now!

 
PHPEdit User Community, © 2008 WaterProof SARL
Powered by PHPEdit