SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Converting native PHP errors to Exceptions

    Hi all,

    I've been playing a little with exceptions, or rather errors, and I'm currently wondering whether to pursue this train of thought.

    Using the following code, I'm able to convert native PHP errors to catchable Exceptions, and I'm unsure as to whether you can see any foreseeable problems or limitations?

    The thought process behind this is that once all errors are now Exceptions, I can create a central, common and standard way of handling application problems.

    Parse errors would still force the script to go pop though...

    Demo:
    PHP Code:
    <?php

    set_error_handler
    (array('error''handle'), E_ALL);

    try
    {
        echo 
    0;
    }
    catch(
    Error $error)
    {
        
    $error = new ErrorExceptionHTMLDecorator($error);
        echo 
    $error->render();
    }
    /*
        WARNING
        Message:    Division by zero
        Code:        2
        Line:        71
        File:        C:\webroot\www\error.php 
    */
    ?>
    Objects:
    PHP Code:
    class Error extends Exception
    {
        protected    
    $types = array(
                        
    E_ERROR                => 'ERROR',
                        
    E_WARNING            => 'WARNING',
                        
    E_PARSE                => 'PARSING ERROR',
                        
    E_NOTICE            => 'NOTICE',
                        
    E_CORE_ERROR        => 'CORE ERROR',
                        
    E_CORE_WARNING        => 'CORE WARNING',
                        
    E_COMPILE_ERROR        => 'COMPILE ERROR',
                        
    E_COMPILE_WARNING    => 'COMPILE WARNING',
                        
    E_USER_ERROR        => 'USER ERROR',
                        
    E_USER_WARNING        => 'USER WARNING',
                        
    E_USER_NOTICE        => 'USER NOTICE',
                        
    E_STRICT            => 'STRICT NOTICE',
                        
    E_RECOVERABLE_ERROR    => 'RECOVERABLE ERROR'
                    
    );

        public function 
    handle($code$string$file$line$context)
        {
            
    $exception = new self($string$code);
            
    $exception->line $line;
            
    $exception->file $file;
            throw 
    $exception;
        }

        public function 
    getType()
        {
            return isset(
    $this->types[$this->getCode()]) ? $this->types[$this->getCode()] : null ;
        }
    }

    class 
    ErrorExceptionHTMLDecorator
    {
        protected 
    $error;

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

        public function 
    render()
        {
            return 
    sprintf(
                
    '
                <h4>%s</h4>
                <strong>Message:</strong>%s<br />
                <strong>Code:</strong>%d<br />
                <strong>Line:</strong>%s<br />
                <strong>File:</strong>%s
                '
    ,
                
    $this->error->getType(),
                
    $this->error->getMessage(),
                
    $this->error->getCode(),
                
    $this->error->getLine(),
                
    $this->error->getFile()
            );
        }

    Thoughts?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    Why not use the SPL ErrorException? Also you won't be catching fatal errors (unless perchance they're "catchable" fatal errors) nor parse errors as you have already pointed out.

    PHP Code:
    <?php

    set_error_handler
    ('error_handler');

    try
    {
        echo 
    0;
    }
    catch(
    ErrorException $error)
    {
        echo new 
    ErrorExceptionHTMLDecorator($error);
    }
    /*
        WARNING
        Message:    Division by zero
        Code:         2
        Line:         71
        File:         C:\webroot\www\error.php 
    */

    function error_handler($errno$errstr$errfile$errline)
    {
        throw new 
    ErrorException($errstr$errno$errno$errfile$errline);
    }


    class 
    ErrorExceptionHTMLDecorator
    {
        protected 
    $error;
        protected static 
    $levels = array(
            
    E_ERROR             => 'ERROR',
            
    E_WARNING           => 'WARNING',
            
    E_PARSE             => 'PARSING ERROR',
            
    E_NOTICE            => 'NOTICE',
            
    E_CORE_ERROR        => 'CORE ERROR',
            
    E_CORE_WARNING      => 'CORE WARNING',
            
    E_COMPILE_ERROR     => 'COMPILE ERROR',
            
    E_COMPILE_WARNING   => 'COMPILE WARNING',
            
    E_USER_ERROR        => 'USER ERROR',
            
    E_USER_WARNING      => 'USER WARNING',
            
    E_USER_NOTICE       => 'USER NOTICE',
            
    E_STRICT            => 'STRICT NOTICE',
            
    E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR'
        
    );

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

        public function 
    render()
        {
            
    $level = isset(self::$levels[$this->error->getSeverity()])
                    ? 
    self::$levels[$this->error->getSeverity()]
                    : 
    'Unknown Error';

            return 
    sprintf(
                
    '
                <h4>%s</h4>
                <strong>Message:</strong>%s<br />
                <strong>Code:</strong>%d<br />
                <strong>Line:</strong>%s<br />
                <strong>File:</strong>%s
                '
    ,
                
    $level,
                
    $this->error->getMessage(),
                
    $this->error->getCode(),
                
    $this->error->getLine(),
                
    $this->error->getFile()
            );
        }
        
        public function 
    __toString() 
        {
            return 
    $this->render();
        }
    }
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Why not use the SPL ErrorException?
    Ah, I had not seen that before Salathe, thanks!

    After thinking about it a little longer, I'm starting to think that maybe there should be a clean difference between errors and exceptions.

    Sure they're of the same ilk, but converting one into the other, only to just split them up again to determine how they're processed seems silly in retrospect.

    Thanks for the cleaner implementation Salathe, always welcome!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •