Overview

Namespaces

  • Application
    • Controller
    • Model
      • DAO
        • Impl
      • Service
        • Impl
  • Define
    • Core
    • DBDrivers
    • Exceptions
    • IUtilities
    • Traits
    • Utilities
  • None

Classes

  • Application\Controller\ApplicationController
  • Application\Controller\ErrorController
  • Application\Controller\IndexController
  • Application\Model\DAO\ApplicationDAO
  • Application\Model\DAO\Impl\IndexDAO
  • Application\Model\Service\ApplicationService
  • Application\Model\Service\Impl\IndexService
  • Define\Core\BaseController
  • Define\Core\BaseDAO
  • Define\Core\BaseModel
  • Define\Core\BaseService
  • Define\Core\Framework
  • Define\Core\ObjectContainer
  • Define\Core\Router
  • Define\Core\View
  • Define\DBDrivers\DatabaseBean
  • Define\DBDrivers\DatabaseFactory
  • Define\DBDrivers\MONGODBDriver
  • Define\DBDrivers\MSSQLDriver
  • Define\DBDrivers\MYSQLDriver
  • Define\DBDrivers\MYSQLIDriver
  • Define\DBDrivers\ORACLEDriver
  • Define\DBDrivers\PDODriver
  • Define\DBDrivers\POSTGREDriver
  • Define\DBDrivers\SQLITEDriver
  • Define\IUtilities\PasswordHelper
  • Define\Utilities\ErrorExceptionHandler
  • Define\Utilities\Localization
  • Define\Utilities\Logger
  • Define\Utilities\Session
  • PHPMailer
  • SMTP

Interfaces

  • Define\Core\IBaseDAO
  • Define\Core\IBaseModel
  • Define\Core\IBaseService
  • Define\Core\IDefine
  • Define\Core\IRouter
  • Define\Core\IView
  • Define\DBDrivers\IDatabase
  • Define\Exceptions\IException
  • Define\Utilities\IUtilities

Traits

  • Define\Traits\SingletonTrait

Exceptions

  • Define\Exceptions\FileNotFoundException
  • Define\Exceptions\FrameworkException
  • Define\Exceptions\LoggerException
  • Define\Exceptions\NullPointerException
  • Define\Exceptions\RuntimeException
  • phpmailerException

Functions

  • __autoload
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace Define\Utilities;
  3: use Define\Traits\SingletonTrait;
  4: 
  5: if(!defined('DIRECT_ACCESS')) {
  6:     die("Direct access is forbidden.");
  7: }
  8: 
  9: /**
 10:  * ERROR EXCEPTION HANDLER
 11:  *
 12:  * This class handles and logs the errors and exception occurs in the project.
 13:  *  
 14:  * @category Define
 15:  * @package Utilities
 16:  * @author Nitesh Apte <me@niteshapte.com>
 17:  * @copyright 2015 Nitesh Apte
 18:  * @version 2.0.0
 19:  * @since 1.0.0
 20:  * @license https://www.gnu.org/licenses/gpl.txt GNU General Public License v3
 21:  * 
 22:  * This program is free software: you can redistribute it and/or modify
 23:  * it under the terms of the GNU General Public License as published by
 24:  * the Free Software Foundation, either version 3 of the License, or
 25:  * (at your option) any later version.
 26:  *
 27:  * This program is distributed in the hope that it will be useful,
 28:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 29:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 30:  * GNU General Public License for more details.
 31:  *
 32:  * You should have received a copy of the GNU General Public License
 33:  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 34:  */
 35: class ErrorExceptionHandler implements IUtilities {
 36:     
 37:     // Singleton instance
 38:     use SingletonTrait;
 39:     
 40:     /**
 41:      * @var $MAXLENGTH Maximum length for backtrace message
 42:      * @see debugBacktrace()
 43:      */
 44:     private $MAXLENGTH = 64;
 45:     
 46:     /**
 47:      * @var $errorType PHP defined errors
 48:      * @see customError()
 49:      */
 50:     private $errorType = array (
 51:             E_ERROR             => 'ERROR',
 52:             E_WARNING           => 'WARNING',
 53:             E_PARSE             => 'PARSING ERROR',
 54:             E_NOTICE            => 'NOTICE',
 55:             E_CORE_ERROR        => 'CORE ERROR',
 56:             E_CORE_WARNING      => 'CORE WARNING',
 57:             E_COMPILE_ERROR     => 'COMPILE ERROR',
 58:             E_COMPILE_WARNING   => 'COMPILE WARNING',
 59:             E_USER_ERROR        => 'USER ERROR',
 60:             E_USER_WARNING      => 'USER WARNING',
 61:             E_USER_NOTICE       => 'USER NOTICE',
 62:             E_STRICT            => 'STRICT',
 63:             E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR',
 64:             E_DEPRECATED        => 'DEPRECATED',
 65:             E_USER_DEPRECATED   => 'USER_DEPRECATED'
 66:     );
 67:     
 68:     /**
 69:      * @var array $exceptionType Custom exception number
 70:      * @see exceptionHandler
 71:      */
 72:     private $exceptionType = array (
 73:             0                   => 'EXCEPTION OCCURRED',
 74:             1001                => 'INCORRECT FORMAT',
 75:             1002                => 'INVALID FILE',
 76:             1003                => 'TYPE MISMATCH',
 77:             1004                => 'FILE NOT FOUND',
 78:             1005                => 'NO FILE'
 79:     );
 80:     
 81:     /**
 82:      * Initiate the handlers
 83:      * 
 84:      * @param none
 85:      * @return void
 86:      */
 87:     private function __construct() {
 88:         //$this->errorType = include 'ErrorCodes.inc';
 89:         //$this->exceptionType = include 'ExceptionCodes.inc';
 90:         $this->enableHandler();
 91:     }
 92:     
 93:     /**
 94:      * Set custom error handler and exception handler
 95:      *
 96:      * @param String $requestFrom
 97:      * @return void
 98:      */
 99:     public function enableHandler() {
100:         \error_reporting(1);
101:         \set_exception_handler(array($this, 'exceptionHandler'));
102:         \set_error_handler(array($this,'errorHandler'), APP_ERROR);
103:         \register_shutdown_function(array($this, 'fatalError'));
104:     }
105:     
106:     /**
107:      * Custom error logging in custom format
108:      *
109:      * @param int $errNo Error number
110:      * @param string $errStr Error string
111:      * @param string $errFile Error file
112:      * @param int $errLine Error line
113:      * @return void
114:      */
115:     public function errorHandler($errNo, $errStr, $errFile, $errLine) {
116:     
117:         if(error_reporting() == 0) {
118:             return;
119:         }
120:         $backTrace = $this->debugBacktrace(2);
121:         
122:         $logMessage = $this->_toStringForLogging($errNo, $errStr, $errFile, $errLine, $backTrace, 'Error', $this->errorType[$errNo]);
123:         $webMessage = $this->_toStringForWeb($errNo, $errStr, $errFile, $errLine, $backTrace, 'Error', $this->errorType[$errNo]);
124: 
125:         $this->debug($logMessage, $webMessage);
126:     }
127:     
128:     /**
129:      * Custom exception handler
130:      * 
131:      * @param \Exception $exception
132:      * @return void
133:      */
134:     public function exceptionHandler(\Exception $exception) {
135:         while ($e = $exception->getPrevious()) {
136:             $exception = $e;
137:         }
138: 
139:         $logMessage = $this->_toStringForLogging($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->getTraceAsString(), 'Exception', $this->exceptionType[$exception->getCode()]);
140:         $webMessage = $this->_toStringForWeb($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->getTraceAsString(), 'Exception', $this->exceptionType[$exception->getCode()]);
141:     
142:         $this->debug($logMessage, $webMessage);
143:     }
144:     
145:     /**
146:      * Display the error
147:      * 
148:      * @param string $logMessage Message for log file
149:      * @param string $webMessage Message to show on browser
150:      * @return void
151:      */
152:     private function debug($logMessage, $webMessage) {
153:         SEND_ERROR_MAIL === TRUE ? error_log($logMessage, 1, ADMIN_ERROR_MAIL, "From: ".SEND_ERROR_FROM."\r\nTo: ".ADMIN_ERROR_MAIL) : "";
154:         ERROR_LOGGING === TRUE ? error_log($logMessage, 3, LOG_FILE_PATH) : "";
155:         echo DEBUGGING === TRUE ? $webMessage : SITE_GENERIC_ERROR_MSG;
156:         MODE == 'DEVELOPMENT' ? exit() : "";
157:     }
158: 
159:     /**
160:      * Build backtrace message
161:      *
162:      * @param $entriesMade Irrelevant entries in debug_backtrace, first two characters
163:      * @return string
164:      */
165:     private function debugBacktrace($entriesMade) {
166:     
167:         $traceArray = debug_backtrace();
168:         $argsDefine = array();
169:     
170:         $traceMessage = '';
171:     
172:         for($i=0;$i<$entriesMade;$i++) {
173:             array_shift($traceArray);
174:         }
175:     
176:         foreach($traceArray as $newArray) {
177:             if(isset($newArray['class'])) {
178:                 $traceMessage .= $newArray['class'].'.';
179:             }
180:             if(!empty($newArray['args'])) {
181:     
182:                 foreach($newArray['args'] as $newValue) {
183:                     if(is_null($newValue)) {
184:                         $argsDefine[] = NULL;
185:                     } elseif(is_array($newValue)) {
186:                         $argsDefine[] = 'Array['.sizeof($newValue).']';
187:                     } elseif(is_object($newValue)) {
188:                         $argsDefine[] = 'Object: '.get_class($newValue);
189:                     } elseif(is_bool($newValue)) {
190:                         $argsDefine[] = $newValue ? 'TRUE' : 'FALSE';
191:                     } else {
192:                         $newValue = (string)@$newValue;
193:                         $stringValue = htmlspecialchars(substr($newValue, 0, $this->MAXLENGTH));
194:                         if(strlen($newValue)>$this->MAXLENGTH) {
195:                             $stringValue = '...';
196:                         }
197:                         $argsDefine[] = "\"".$stringValue."\"";
198:                     }
199:                 }
200:             }
201:             $traceMessage .= $newArray['function'].'('.implode(',', $argsDefine).')';
202:             $lineNumber = (isset($newArray['line']) ? $newArray['line']:"unknown");
203:             $fileName = (isset($newArray['file']) ? $newArray['file']:"unknown");
204:     
205:             $traceMessage .= sprintf(" # line %4d. file: %s", $lineNumber, $fileName, $fileName);
206:             $traceMessage .= "\n";
207:         }
208:         return $traceMessage;
209:     }
210:     
211:     /**
212:      * Method to catch fatal and parse error
213:      *
214:      * @param none
215:      * @return void
216:      */
217:     public function fatalError() {
218:         $lastError =  error_get_last();
219:         if($lastError['type'] == 1 || $lastError['type'] == 4 || $lastError['type'] == 16 || $lastError['type'] == 64 || $lastError['type'] == 256 || $lastError['type'] == 4096) {
220:             $this->errorHandler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']);
221:         }
222:     }
223:     
224:     /**
225:      * Decorate the message for browser
226:      *
227:      * @param int $errNo
228:      * @param string $errStr
229:      * @param string $errFile
230:      * @param int $errLine
231:      * @param string $backTrace
232:      * @param string $category
233:      * @return string
234:      */
235:     private function _toStringForWeb($errNo, $errStr, $errFile, $errLine, $backTrace, $category, $type) {
236:         $css = <<<EOT
237:         <style>
238:         .errormessage {
239:             margin:0px;padding:0px;
240:             width:100%;
241:         }
242:         .errormessage table{
243:             border-collapse: collapse;
244:             border-spacing: 0;
245:             width:100%;
246:             margin:0px;padding:0px;
247:         }
248:         .errormessage tr:last-child td:last-child {
249:             -moz-border-radius-bottomright:0px;
250:             -webkit-border-bottom-right-radius:0px;
251:             border-bottom-right-radius:0px;
252:         }
253:         .errormessage table tr:first-child td:first-child {
254:             -moz-border-radius-topleft:0px;
255:             -webkit-border-top-left-radius:0px;
256:             border-top-left-radius:0px;
257:         }
258:         .errormessage table tr:first-child td:last-child {
259:             -moz-border-radius-topright:0px;
260:             -webkit-border-top-right-radius:0px;
261:             border-top-right-radius:0px;
262:         }
263:         .errormessage tr:last-child td:first-child{
264:             -moz-border-radius-bottomleft:0px;
265:             -webkit-border-bottom-left-radius:0px;
266:             border-bottom-left-radius:0px;
267:         }
268:         .errormessage tr:hover td{
269:     
270:         }
271:         .errormessage tr:nth-child(odd){
272:             background-color:#e5e5e5;
273:         }
274:         .errormessage tr:nth-child(even) {
275:             background-color:#ffffff;
276:         }.errormessage td {
277:             vertical-align:middle;
278:             border:1px solid #000000;
279:             border-width:0px 1px 1px 0px;
280:             text-align:left;
281:             padding:5px;
282:             font-size:12px;
283:             font-family:Arial;
284:             font-weight:normal;
285:             color:#000000;
286:         }.errormessage tr:last-child td{
287:             border-width:0px 1px 0px 0px;
288:         }.errormessage tr td:last-child{
289:             border-width:0px 0px 1px 0px;
290:         }.errormessage tr:last-child td:last-child{
291:             border-width:0px 0px 0px 0px;
292:         }
293:         .errorhead {
294:             font: 20px Arial;
295:             margin: 5px 0 10px 0;
296:             font-weight: bold;
297:         }
298:         </style>
299: EOT;
300:     
301:         $errorMessage = "<title>Website Generic Error and Exception Application - {$type}</title><div class='errorhead'>Website Generic Error and Exception Application</div><div class='errormessage'><table border=1>";
302:         $errorMessage .= "<tr><td><b>CATEGORY : </b></td><td><font color='red'>{$category}</font></td></tr>";
303:         $errorMessage .= "<tr><td><b>ERROR NO : </b></td><td><font color='red'>{$errNo}</font></td></tr>";
304:         $errorMessage .= "<tr><td><b>ERROR TYPE : </b></td><td><i><b><font color='red'>{$type}</font></b></i></td></tr>";
305:         $errorMessage .= "<tr><td><b>TEXT : </b></td><td><font color='red'>{$errStr}</font></td></tr>";
306:         $errorMessage .= "<tr><td><b>LOCATION : </b></td><td><font color='red'>{$errFile}</font>, <b>line</b> {$errLine}, at ".date("F j, Y, g:i a")."</td></tr>";
307:         $errorMessage .= "<tr><td width='120px'><b>Showing Backtrace : </b></td><td>{$backTrace} </td></tr></table></div>";
308:         $webMessage = str_replace("#", "<br />", $errorMessage);
309:         $webMessage = $css.$webMessage;
310:         return $webMessage;
311:     }
312:     
313:     /**
314:      * Decorate the message for log file
315:      *
316:      * @param int $errNo
317:      * @param string $errStr
318:      * @param string $errFile
319:      * @param int $errLine
320:      * @param string $backTrace
321:      * @param string $category
322:      * @return string
323:      */
324:     private function _toStringForLogging($errNo, $errStr, $errFile, $errLine, $backTrace, $category, $type) {
325:         $logMessage = "=====================================================================================================================================\n";
326:         $logMessage .= "Website Generic Error!\n";
327:         $logMessage .= "=====================================================================================================================================\n";
328:         $logMessage .= "CATEGORY : {$category}\n";
329:         $logMessage .= "=====================================================================================================================================\n";
330:         $logMessage .= "ERROR NO : {$errNo}\n";
331:         $logMessage .= "=====================================================================================================================================\n";
332:         $logMessage .= "ERROR TYPE : {$type}\n";
333:         $logMessage .= "=====================================================================================================================================\n";
334:         $logMessage .= "TEXT : {$errStr}\n";
335:         $logMessage .= "=====================================================================================================================================\n";
336:         $logMessage .= "LOCATION : {$errFile}, line {$errLine}, at ".date("F j, Y, g:i a")."\n";
337:         $logMessage .= "=====================================================================================================================================\n";
338:         $logMessage .= "Showing Backtrace : \n{$backTrace} \n";
339:         $logMessage .= "=====================================================================================================================================\n\n";
340:         return $logMessage;
341:     }
342: }
API documentation generated by ApiGen