1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124:
| <?php /** * Main Application */
error_reporting(E_ALL);
/** * Small Exception Handler for nice * formatted output */ function exception_handler($exc) { if (method_exists($exc, 'getFormattedMessage')) { echo $exc->getFormattedMessage(); } else { echo '<h1>Error ' . $exc->getCode() . '</h1>'; echo '<p>' . htmlspecialchars($exc->getMessage()) . '</p>'; echo '<pre>file: ' . $exc->getFile(); echo ', line ' . $exc->getLine() . '</pre>'; $traceback = $exc->getTrace(); if (count($traceback)) { echo '<h2>Traceback</h2>'; foreach ($traceback as $tb) { echo '<pre>file: ' . $tb['file'] . "\n"; echo 'line: ' . $tb['line'] . "\n"; echo 'function: ' . $tb['function']; if (count($tb['args'])) { echo "\nargs: \n "; echo htmlspecialchars(implode("\n ", $tb['args'])); } echo '</pre>'; } } } } set_exception_handler('exception_handler');
/** * Example Exceptions */ class ImproperConfiguration extends Exception { public function __construct($msg=false) { if ($msg === false) { $msg = 'unknown reason'; } parent::__construct('Improper Configuration: ' . $msg, 310); }
public function getFormattedMessage() { return '<h1>Improper Configuration</h1>'. "\n". '<p>' . $this->getMessage() . '</p>'; } }
class PageNotFound extends Exception { private $app;
public function __construct($app) { $this->app = $app; parent::__construct('Page Not Found', 404); }
public function getFormattedMessage() { return '<h1>Page Not Found</h1>' . '<p>The requested path ' . $this->app->request . ' was not found on the server.</p>'; } }
/** * Application */ class Application { public $request; public $params; public $form; public function __construct() { if (isset($_SERVER['PATH_INFO'])) { $this->request = $_SERVER['PATH_INFO']; } else { $this->request = '/'; } $this->params = Array(); $this->form = Array(); $quotes = get_magic_quotes_gpc(); foreach ($_GET as $key => $value) { if ($quotes) { $this->params[$key] = $value; } else { $this->params[$key] = stripslashes($value); } } foreach ($_POST as $key => $value) { if ($quotes) { $this->form[$key] = $value; } else { $this->form[$key] = stripslashes($value); } } }
public function execute() { if ($this->request == '/') { throw new ImproperConfiguration('argh. something went wrong in the config.'); } else { throw new PageNotFound($this); } } }
function on_request() { $app = new Application(); $app->execute(); } on_request(); ?> |
Ich hoffe das hilft dir.
Nochwas zur Erklärung. Einen Teil davon hab ich aus alten Code Schnippseln von mir genommen und entschlackt. Wenn du einen Fehler wirfst schaut er zuerst, obs ein
getFormattedMessage gibt, dann ruft er das auf und gibts zurück, ansonsten präsentiert er dir einen vereinfachten Traceback. Die Funktion
on_request ist eigentlich sinnlos, aber eaccellerator hat damit einen Freude
Das Original von mir hatte keine Funktion
getFormattedMessage, sondern ein
getExceptionContext, dass ein Array für smarty zurückgegeben hat, das wurde dann direkt in ein Template gerendert.