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:
<?php
class ExceptionHandler
{
private $exception;
public function handle($exception)
{
$this->exception = $exception;
$this->clean_output_buffer();
$this->log();
$this->display();
$this->destroy_app();
}
private function clean_output_buffer()
{
AppContext::get_response()->clean_output();
}
private function log()
{
ErrorHandler::add_error_in_log($this->exception->getMessage(), Debug::get_stacktrace_as_string(0, $this->exception), E_USER_ERROR);
}
private function display()
{
if (Debug::is_debug_mode_enabled())
{
$this->raw_display();
}
else
{
$controller = $this->prepare_controller();
$this->send_response($controller);
}
}
private function destroy_app()
{
Environment::destroy();
exit;
}
private function raw_display()
{
if (Debug::is_debug_mode_enabled())
{
Debug::fatal($this->exception);
}
else
{
echo ErrorHandler::FATAL_MESSAGE;
}
}
private function prepare_controller()
{
$title = LangLoader::get_message('error', 'status-messages-common');
if ($this->exception !== null && Debug::is_debug_mode_enabled())
{
$message = TextHelper::htmlspecialchars($this->exception->getMessage()) . '<br /><br /><i>' .
$this->exception->getFile() . ':' . $this->exception->getLine() .
'</i><div class="spacer"></div>' .
Debug::get_stacktrace_as_string(0, $this->exception);
$title .= ' ' . $this->exception->getCode();
}
else
{
$message = TextHelper::htmlspecialchars(LangLoader::get_message('process.error', 'status-messages-common'));
}
$controller = new UserErrorController($title, $message, UserErrorController::FATAL);
return $controller;
}
private function send_response(Controller $controller)
{
try
{
$this->integrated_display($controller);
}
catch (Exception $exception)
{
$this->clean_output_buffer();
$this->raw_display();
}
}
private function integrated_display(Controller $controller)
{
$request = AppContext::get_request();
$response = $controller->execute($request);
$response->send();
}
}
?>