1: <?php defined('_JOOS_CORE') or exit();
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('_JOOS_CORE') or exit();
18:
19: 20: 21: 22:
23: class joosException extends Exception
24: {
25: const CONTEXT_RADIUS = 10;
26:
27: public function __construct($message = '', array $params = array())
28: {
29: joosRequest::send_headers_by_code(503);
30:
31: parent::__construct(strtr($message, $params));
32:
33: if (isset($params[':error_file'])) {
34: $this->file = $params[':error_file'];
35: }
36:
37: if (isset($params[':error_line'])) {
38: $this->line = $params[':error_line'];
39: }
40:
41: if (isset($params[':error_code'])) {
42: $this->code = $params[':error_code'];
43: }
44:
45:
46: }
47:
48: private function get_file_context()
49: {
50: $file = $this->getFile();
51: $line_number = $this->getLine();
52:
53: $context = array();
54: $i = 0;
55: foreach (file($file) as $line) {
56: $i++;
57: if ($i >= $line_number - self::CONTEXT_RADIUS && $i <= $line_number + self::CONTEXT_RADIUS) {
58: if ($i == $line_number) {
59: $context[] = ' >> ' . $i . "\t" . $line;
60: } else {
61: $context[] = "\t" . $i . "\t" . $line;
62: }
63: }
64: if ($i > $line_number + self::CONTEXT_RADIUS) {
65: break;
66: }
67: }
68:
69: return "\n" . implode("", $context);
70: }
71:
72: public function __toString()
73: {
74:
75: !ob_get_level() ? : ob_end_clean();
76:
77: parent::__toString();
78: echo joosRequest::is_ajax() ? $this->to_json() : $this->show();
79: die();
80: }
81:
82: private function show()
83: {
84: return JDEBUG ? $this->create() : $this->render();
85: }
86:
87: private function create()
88: {
89: if (headers_sent()) {
90: !ob_get_level() ? : ob_end_clean();
91: } else {
92: joosRequest::send_headers('Content-type: text/html; charset=UTF-8');
93: }
94:
95: $message = nl2br($this->getMessage());
96: $result = <<<HTML
97: <style>
98: body { background-color: #fff; color: #333; }
99: body, p, ol, ul, td { font-family: verdana, arial, helvetica, sans-serif; font-size: 13px; line-height: 25px; }
100: pre { background-color: #eee; padding: 10px; font-size: 11px; line-height: 18px; }
101: a { color: #000; }
102: a:visited { color: #666; }
103: a:hover { color: #fff; background-color:#000; }
104: </style>
105: <div style="width:99%; position:relative">
106: <h2 id='Title'>{$message}</h2>
107: <div id="Context" style="display: block;"><h3>Ошибка с кодом {$this->getCode()} в файле '{$this->getFile()}' в строке {$this->getLine()}:</h3><pre>{$this->prepare($this->get_file_context())}</pre></div>
108: <div id="Trace"><h2>Стэк вызовов</h2><pre>{$this->getTraceAsString()}</pre></div>
109: HTML;
110: $result .= "</div></div>";
111:
112: return $result;
113: }
114:
115: protected function prepare($content)
116: {
117: return htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8');
118: }
119:
120: 121: 122: 123: 124:
125: private function to_json()
126: {
127: $response = array('code' => ($this->getCode() != 0) ? $this->getCode() : 503, 'message' => $this->getMessage());
128:
129: return joosText::json_encode($response);
130: }
131:
132: private function render()
133: {
134: $message = $this->message;
135:
136: $file = (!JDEBUG && $this->code != 503) ? sprintf('%s/app/templates/system/500.php', JPATH_BASE) : sprintf('%s/app/templates/system/exception.php', JPATH_BASE);
137:
138: require $file;
139: }
140:
141: public static function error_handler($code, $message, $file, $line)
142: {
143: throw new joosException('Ошибка :message! <br /> Код: <pre>:error_code</pre> Файл: :error_file<br />Строка :error_line', array(':message' => $message, ':error_code' => $code, ':error_file' => $file, ':error_line' => $line));
144: }
145:
146: }
147:
148: 149: 150: 151:
152: class joosUserException extends joosException
153: {
154: public function __construct($message = '', array $params = array())
155: {
156: $this->code = 503;
157:
158: parent::__construct(strtr($message, $params));
159:
160: $this->__toString();
161: }
162:
163: }
164: