Skip to content

Commit fb1ef0f

Browse files
committed
Error handling added
1 parent 0265865 commit fb1ef0f

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

framework/Libraries/Application.php

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function run()
4646
die("{$logDir} is not writable");
4747
}
4848

49+
// Set Error Handler
50+
(new \nanophp\Libraries\Error)->init();
51+
4952
// Set default timezone if it is set in config file
5053
if ($timezone = Config::instance()->get('/timezone')) {
5154
date_default_timezone_set($timezone);

framework/Libraries/Error.php

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace nanophp\Libraries;
4+
5+
use nanophp\Libraries\Config;
6+
use Monolog\Logger;
7+
8+
/**
9+
* Class: Error
10+
*
11+
* Error handler class of NanoPHP
12+
*
13+
* @final
14+
*/
15+
final class Error
16+
{
17+
/**
18+
* init
19+
*
20+
* Register error handling functions and set display errors
21+
*/
22+
public function init()
23+
{
24+
// Check if we should display errors
25+
if (Config::instance()->get('production')) {
26+
ini_set('display_errors', 0);
27+
ini_set('display_startup_errors', 0);
28+
}
29+
30+
// Set error reporting if there is a reporting level set in config file
31+
if (($error_reporting = Config::instance()->get('error/reporting', false)) !== False) {
32+
error_reporting($error_reporting);
33+
}
34+
35+
// Set a custom error handler
36+
set_error_handler([$this, 'errorHandler']);
37+
38+
// Set a custom exception handler
39+
set_exception_handler([$this, 'exceptionHanler']);
40+
41+
// Set a custom shutdown handler
42+
register_shutdown_function([$this, 'shutdownHandler']);
43+
}
44+
45+
/**
46+
* shutdownHandler
47+
*
48+
* Custom shutdown handler
49+
*/
50+
public function shutdownHandler()
51+
{
52+
// Check what caused the application to shutdown
53+
$error = error_get_last();
54+
55+
if ($error) {
56+
// It seems and error occured
57+
// Get error type
58+
$type = $this->_getError($error['type']);
59+
60+
// Log error
61+
\nanophp\Libraries\Application::logger()->log($type, "{$error['message']} on {$error['file']}:{$error['line']}");
62+
}
63+
}
64+
65+
/**
66+
* exceptionHanler
67+
*
68+
* Custom exception handler
69+
*
70+
* @param Exception $e
71+
*/
72+
public function exceptionHanler(Exception $e)
73+
{
74+
// Log exception
75+
\nanophp\Libraries\Application::logger()->log(Logger::ERROR, "{$e->getMessage()} on {$e->getFile()}:{$e->getLine()}");
76+
}
77+
78+
/**
79+
* errorHandler
80+
*
81+
* Custom error handler function
82+
*
83+
* @param int $no
84+
* @param string $str
85+
* @param string $file
86+
* @param int $line
87+
* @param string $context
88+
*/
89+
public function errorHandler($no, $str, $file, $line, $context)
90+
{
91+
// Check if error is included in error reporting
92+
if (!(error_reporting() & $no)) {
93+
return;
94+
}
95+
96+
// Get error type
97+
$type = $this->_getError($no);
98+
99+
// Log error
100+
\nanophp\Libraries\Application::logger()->log($type, "{$str} on {$file}:{$line}");
101+
}
102+
103+
/**
104+
* _getError
105+
*
106+
* Return Error type
107+
*
108+
* @param int $type
109+
*/
110+
protected function _getError($type)
111+
{
112+
switch ($type) {
113+
case E_WARNING: // 2 //
114+
return Logger::WARNING;
115+
case E_NOTICE: // 8 //
116+
return Logger::NOTICE;
117+
case E_CORE_WARNING: // 32 //
118+
return Logger::WARNING;
119+
case E_USER_WARNING: // 512 //
120+
return Logger::WARNING;
121+
case E_USER_NOTICE: // 1024 //
122+
return Logger::NOTICE;
123+
case E_DEPRECATED: // 8192 //
124+
return Logger::WARNING;
125+
case E_USER_DEPRECATED: // 16384 //
126+
return Logger::WARNING;
127+
}
128+
129+
return Logger::ERROR;
130+
}
131+
}

0 commit comments

Comments
 (0)