Accueil > > > [PHP5] EXCEPTIONERROR PACKAGE : TRANSFORMER TOUTES LES ERREURS PHP EN EXCEPTIONS INTERCEPTABLES
[PHP5] EXCEPTIONERROR PACKAGE : TRANSFORMER TOUTES LES ERREURS PHP EN EXCEPTIONS INTERCEPTABLES
Information sur la source
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
- <?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);
- }
- }
- }
- ?>
<?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 :-)
Historique
- 18 octobre 2007 12:52:13 :
- Petite modif pour prendre en compte les erreurs non encore gérées par ce package (sait-on jamais...), et pour pouvoir appeler la méthode de manière statique.
- 25 octobre 2007 23:29:10 :
- Ajout d'une précision suite à la remarque de Celeg
- 01 janvier 2008 10:15:34 :
- RAS
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Handler d'une table [ par apz ]
salut, que veut dire le message d'erreur suivant : Citation: Reçu l'erreur 127 du handler de la table Merci
Handler de la table [ par apz ]
salut, que veut dire le message d'erreur suivant : Citation: Reçu l'erreur 127 du handler de la table Merci
Problème syntaxique sous PHP [ par olive59 ]
Débutant sous PHP et ayant tendance à mélanger les différents langages que j'ai essayé d'assimiler, quelqu'un pourrait-il me renseigner sur le problèm
:::::: URGENT !!! ENVOI D'IMAGE PAR FTP !!!! ERREUR :::::: [ par kkz_mil3k ]
j'essaie d'nevoyer un fichier image gif ou jpg sur un ftp via ce formulaire php :------------------------------------------- //**connecte au ftp sc
Message ERREUR Suite tentative de transfert d'un fichier [ par David ]
Il en résulte le message d'erreur suivant : Warning: Unable to create '../occas/1.jpg': Permission denied in /home4/eq9846/html/test/prive/admin/edi_o
Astuce du jour #1: Comment configurer une erreur de la base de donné MySql [ par SmallToad ]
Quand vous avez de des erreurs de la base de donnée, êtes-vous déjà demander comment configurer le message d'erreur de la base de donnée MySql C'est
aidez moi!!! [ par mic29 ]
Voila je débute dans le milieu et je viens de faire mon forum.J'ai une erreur mais je ne voie trop comment y remédier.A chaque fois que je poste un me
aidez moi!!! [ par mic29 ]
Voila je débute dans le milieu et je viens de faire mon forum.J'ai une erreur mais je ne voie trop comment y remédier.A chaque fois que je poste un me
PROBLEME SIMPLE [ par g0belin ]
sa me repond sa---------------------------ERREUR--------------------Réponse serveur SQL : You have an error in your SQL syntax near '@msn.com, 1234567
pb avec ma base [ par Xime ]
salut :)j'essaye de me connecter a ma base oracle en faisant un truc tout con mais g un probleme et je ne voi pas ou !voila ce ke je fais : <?php$c
|
Derniers Blogs
SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|