[php5] exceptionerror package : transformer toutes les erreurs php en exceptions interceptables

Description

Ce package permet de transformer toutes les erreurs PHP en exceptions interceptables.
En clair, sur toutes les fonctions PHP (ou vos fonctions renvoyant des erreurs utilisateurs par exemple via trigger_error()), vous pouvez grâce à ce package faire des try catch.

Le code est documenté, et un exemple est disponible dans le fichier index.php.

Source / Exemple :


<?php
/**

  • @package package.exceptions.errors
  • @author Johan Barbier <barbier_johan@hotmail.com>
  • @version 20071017
  • @desc package transforming all php errors into catchable exceptions
*
  • /
/**
  • @name exceptionErrorGeneric
  • @desc Generic exceptionError class. Overload Exception::__toString() method and Exception::__construct() method
*
  • /
abstract class exceptionErrorGeneric extends Exception { /**
  • @desc Error type
*
  • @var string
  • /
protected $sType; /**
  • @desc Error file
*
  • @var string
  • /
protected $sErrFile; /**
  • @desc Error line
*
  • @var int
  • /
protected $iErrLine; /**
  • @desc Error context
*
  • @var mixed
  • /
protected $mVars; /**
  • @desc is context enabled or not
*
  • @var boolean
  • /
protected $bContext; /**
  • @desc constructor
*
  • @param constant $cErrno
  • @param string $sErrStr
  • @param string $sErrFile
  • @param int $iErrLine
  • @param mixed $mVars
  • @param boolean $bContext
  • /
public function __construct($cErrno, $sErrStr, $sErrFile, $iErrLine, $mVars, $bContext = false) { parent::__construct($sErrStr, $cErrno); $this->sErrFile = $sErrFile; $this->iErrLine = $iErrLine; $this->mVars = $mVars; $this->bContext = $bContext; } /**
  • @desc Exception::__toString() overloading
*
  • @return string
  • /
public function __toString() { $sMsg = '<strong>'.$this->sType.'</strong><br />'; $sMsg .= $this->getMessage().'['.$this->getCode().']<br />'; $sMsg .= '<em>File</em> : '.$this->sErrFile.' on line '.$this->iErrLine.'<br />'; $sMsg.= '<em>Trace</em> :<br />'.$this->getTraceAsString().'<br />'; if(true === $this->bContext) { $sMsg.= '<em>Context</em> :<br />'.print_r($this->mVars, true); } return $sMsg; } } /**
  • @desc exceptionErrors for Fatal errors
*
  • /
class exceptionErrorError extends exceptionErrorGeneric { protected $sType = 'Fatal error'; } /**
  • @desc exceptionErrors for Warnings
*
  • /
class exceptionErrorWarning extends exceptionErrorGeneric { protected $sType = 'Warning'; } /**
  • @desc exceptionErrors for Parse errors
*
  • /
class exceptionErrorParseError extends exceptionErrorGeneric { protected $sType = 'Parse error'; } /**
  • @desc exceptionErrors for Notice
*
  • /
class exceptionErrorNotice extends exceptionErrorGeneric { protected $sType = 'Notice'; } /**
  • @desc exceptionErrors for Core errors
*
  • /
class exceptionErrorCoreError extends exceptionErrorGeneric { protected $sType = 'Core error'; } /**
  • @desc exceptionErrors for Core warnings
*
  • /
class exceptionErrorCoreWarning extends exceptionErrorGeneric { protected $sType = 'Core warning'; } /**
  • @desc exceptionErrors for Compile errors
*
  • /
class exceptionErrorCompileError extends exceptionErrorGeneric { protected $sType = 'Compile error'; } /**
  • @desc exceptionErrors for Compile warnings
*
  • /
class exceptionErrorCompileWarning extends exceptionErrorGeneric { protected $sType = 'Compile warning'; } /**
  • @desc exceptionErrors for User errors
*
  • /
class exceptionErrorUserError extends exceptionErrorGeneric { protected $sType = 'User error'; } /**
  • @desc exceptionErrors for User warnings
*
  • /
class exceptionErrorUserWarning extends exceptionErrorGeneric { protected $sType = 'User warning'; } /**
  • @desc exceptionErrors for User notices
*
  • /
class exceptionErrorUserNotice extends exceptionErrorGeneric { protected $sType = 'User notice'; } /**
  • @desc exceptionErrors for Strict errors
*
  • /
class exceptionErrorStrictError extends exceptionErrorGeneric { protected $sType = 'Strict error'; } /**
  • @desc exceptionErrors for not handled yet errors
*
  • /
class exceptionErrorNotHandledYet extends exceptionErrorGeneric { protected $sType = 'Not handled yet'; } /**
  • @desc error handler, calling correct exceptionError type
*
  • /
class exceptionErrorHandler { /**
  • @desc translation between context error and exceptionError type of class
*
  • @var array
  • /
public static $aTrans = array ( E_ERROR => 'exceptionErrorError', E_WARNING => 'exceptionErrorWarning', E_PARSE => 'exceptionErrorParseError', E_NOTICE => 'exceptionErrorNotice', E_CORE_ERROR => 'exceptionErrorCoreError', E_CORE_WARNING => 'exceptionErrorCoreWarning', E_COMPILE_ERROR => 'exceptionErrorCompileError', E_COMPILE_WARNING => 'exceptionErrorCompileWarning', E_USER_ERROR => 'exceptionErrorUserError', E_USER_WARNING => 'exceptionErrorUserWarning', E_USER_NOTICE => 'exceptionErrorUserNotice', E_STRICT => 'exceptionErrorStrictError' ); /**
  • @desc is context enabled or not
*
  • @var boolean
  • /
public static $bContext = false; /**
  • @desc constructor, optional bContext boolean can be given if you want context to be displayed or not
*
  • @param boolean $bContext (optional, default = false)
  • /
public function __construct($bContext = false) { self::$bContext = $bContext; set_error_handler(array ($this, 'errorHandler')); } /**
  • @desc error handler
*
  • @param constant $cErrno
  • @param string $sErrStr
  • @param string $sErrFile
  • @param int $iErrLine
  • @param mixed $mVars
  • /
public function errorHandler ($cErrno, $sErrStr, $sErrFile, $iErrLine, $mVars) { if(!isset(self::$aTrans[$cErrno])) { throw new exceptionErrorNotHandledYet($cErrno, $sErrStr, $sErrFile, $iErrLine, $mVars, self::$bContext); } else { throw new self::$aTrans[$cErrno]($cErrno, $sErrStr, $sErrFile, $iErrLine, $mVars, self::$bContext); } } } ?>

Conclusion :


A noter que seules les erreurs gérées par set_error_handler() ne sont interceptées, et donc les suivantes ne peuvent m'être :
E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING

Juste pour dire parce que c'est toujours agréable : ce code a terminé 2ème des Innovation Awards de Novembre 2007 de phpclasses.org :-)

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.