PHP Classes

File: src/Generics/Util/ExceptionErrorHandler.php

Recommend this page to a friend!
  Classes of Maik Greubel   PHP Generics   src/Generics/Util/ExceptionErrorHandler.php   Download  
File: src/Generics/Util/ExceptionErrorHandler.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Generics
Framework for accessing streams, sockets and logs
Author: By
Last change: Update of src/Generics/Util/ExceptionErrorHandler.php
Date: 6 months ago
Size: 2,236 bytes
 

Contents

Class file image Download
<?php

/**
 * This file is part of the PHP Generics package.
 *
 * @package Generics
 */
namespace Generics\Util;

use
Generics\DeprecatedException;
use
Generics\NoticeException;
use
Generics\RecoverableErrorException;
use
Generics\UserDeprecatedException;
use
Generics\UserErrorException;
use
Generics\UserNoticeException;
use
Generics\UserWarningException;
use
Generics\WarningException;
use
ErrorException;

/**
 * This class provides a custom error handler for exception handling rather than internal error handling
 *
 * @author Maik Greubel <greubel@nkey.de>
 *
 */
class ExceptionErrorHandler
{

    public function
__construct()
    {
       
$this->setErrorHandler();
    }

    private function
setErrorHandler()
    {
       
set_error_handler(array(
           
$this,
           
'errorHandler'
       
), E_ALL | E_STRICT);
    }

    public function
errorHandler(int $err_severity, string $err_msg, string $err_file, int $err_line)
    {
        if (
0 === error_reporting()) {
            return
false;
        }
       
        switch (
$err_severity) {
            case
E_WARNING:
                throw new
WarningException($err_msg, 0, $err_severity, $err_file, $err_line);
            case
E_NOTICE:
                throw new
NoticeException($err_msg, 0, $err_severity, $err_file, $err_line);
            case
E_USER_ERROR:
                throw new
UserErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
            case
E_USER_WARNING:
                throw new
UserWarningException($err_msg, 0, $err_severity, $err_file, $err_line);
            case
E_USER_NOTICE:
                throw new
UserNoticeException($err_msg, 0, $err_severity, $err_file, $err_line);
            case
E_RECOVERABLE_ERROR:
                throw new
RecoverableErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
            case
E_DEPRECATED:
                throw new
DeprecatedException($err_msg, 0, $err_severity, $err_file, $err_line);
            case
E_USER_DEPRECATED:
                throw new
UserDeprecatedException($err_msg, 0, $err_severity, $err_file, $err_line);
            default:
                throw new
ErrorException($err_msg, 0, $err_severity, $err_file, $err_line);
        }
    }
}