1 <?php
2 3 4 5 6 7
8
9 namespace Hawk;
10
11 12 13 14 15
16 final class ErrorHandler extends Singleton{
17
18 19 20
21 protected static $instance;
22
23 24 25 26 27 28 29 30 31
32 public function error($no, $str, $file, $line, $context){
33 if (!(error_reporting() & $no)) {
34
35 return;
36 }
37
38 switch($no){
39 case E_NOTICE :
40 case E_USER_NOTICE:
41 $level = "info";
42 $icon = 'info-circle';
43 $title = "PHP Notice";
44 App::logger()->notice($str);
45 break;
46
47 case E_STRICT :
48 $level = "info";
49 $icon = 'info-circle';
50 $title = "PHP Strict";
51 App::logger()->notice($str);
52 break;
53
54 case E_DEPRECATED :
55 case E_USER_DEPRECATED :
56 $level = "info";
57 $icon = 'info-circle';
58 $title = "PHP Deprecated";
59 App::logger()->notice($str);
60 break;
61
62 case E_USER_WARNING :
63 case E_WARNING :
64 $level = "warning";
65 $icon = "exclamation-triangle";
66 $title = "PHP Warning";
67 App::logger()->warning($str);
68 break;
69
70 default:
71 $level = "danger";
72 $icon = "exclamation-circle";
73 $title = "PHP Error";
74 App::logger()->error($str);
75 break;
76 }
77
78 $param = array(
79 'level' => $level,
80 'icon' => $icon,
81 'title' => $title,
82 'message' => $str,
83 'trace' => array(
84 array('file' => $file, 'line' => $line)
85 )
86 );
87
88 if(!App::response()->getContentType() === "json") {
89 App::response()->setBody($param);
90 throw new AppStopException();
91 }
92 else{
93 echo View::make(Theme::getSelected()->getView('error.tpl'), $param);
94 }
95 }
96
97
98 99 100 101 102
103 public function exception($e){
104 $param = array(
105 'level' => 'danger',
106 'icon' => 'excalamation-circle',
107 'title' => get_class($e),
108 'message' => $e->getMessage(),
109 'trace' => $e->getTrace()
110 );
111
112 App::logger()->error($e->getMessage());
113 if(App::response()->getContentType() === "json") {
114 App::response()->setBody($param);
115 }
116 else{
117 App::response()->setBody(View::make(Theme::getSelected()->getView('error.tpl'), $param));
118 App::response()->end();
119 }
120 }
121
122 123 124
125 public function fatalError(){
126 $error = error_get_last();
127
128 if($error !== null) {
129 $errno = $error["type"];
130 $errfile = $error["file"];
131 $errline = $error["line"];
132 $errstr = $error["message"];
133
134 $this->error($errno, $errstr, $errfile, $errline, array());
135 }
136 }
137
138 }
139