Re: phpbuilder - Error Handling
| From: | Sterling Hughes | Date: | Tue, 18 Jul 2000 21:21:37 +0000 |
| Subject: | Re: phpbuilder - Error Handling | ||
| References: | 1 | Groups: | php.pear |
| Request: | Send a blank email to php-pear+get-146@lists.php.net to get a copy of this message | ||
> Hi all,
>
> here some ideas on error handling based on what I have seen in the
> phpbuilder article
> http://www.phpbuilder.com/columns/sean20000712.php3?page=3. I
> guess it
> shown only a small subset of your ideas Stig, does it?
>
> I like the idea of creating error objects very much and I played with it
> in my last project. I adopted quite a lot of the java way. All error
> classes are inherited from a common baseclass "exception".
>
> instance variables:
>
> $CLASSNAME - overriden, used to classify exceptions (IO, User, ...)
>
> $ERROR_MESSAGE_PREFIX - overriden, some prefix added to the exception
>
> $ERROR_PREPEND - some prepend and append for PHP3 users that do not
> $ERROR_APPEND - have trigger_error() to customize the output
>
> $message - error message itself
> $file - Filename where the error occured
> $line - Line number where the error occured, method name is
> not neccessary as long as you have the line number
>
>
> $DIE_ON_ERROR - for debugging purpose it should be there
> $AUTO_PRINT_ERROR - same as $DIE_ON_ERROR
>
> $LEVEL - error level used when trigger_error() gets called
> $TRIGGER_ERROR - turns the call to trigger_error() on and off
>
>
> I like having $DIE_ON_ERROR and $AUTO_PRINT_ERROR very much because it
> makes debugging easy. Just turn it on an you'll see all messages. All
> other variables are self explaining.
>
> methods:
>
> void constructor ( string $message, [string $file, int $line])
>
> Sets some instance variables (message, file, line) and
> calls trigger_error(), printLocalizedMessage() and die()
> if necessary.
>
> string getMessage( void )
>
> Returns the current error message. Uses ERROR_PREPEND,
> ERROR_MESSAGE_PREFIX, message and ERROR_APPEND to
> build the message.
>
> string getLocalizedMessage ( void )
>
> Same as getMessage() plus line numbwe and filename.
>
> string getType( void )
>
> Returns the $CLASSNAME. The classname should
> always start with "Exception" or maybe "Error"
> followed by a descriptive name. The $CLASSNAME
> allows you to distinguish beetween a valid
> return type and an error...
>
>
> What does a derived class MyPackageException" look like?
>
> instance variables:
>
> $CLASSNAME = "ExceptionMyPackage";
> $ERROR_MESSAGE_PREPEND = "Error in my package: ";
> $LEVEL = USER_ERROR;
>
> [$ERROR_PREPEND]
> [$ERROR_APPEND]
>
> methods:
>
> void constructor ( string $message [, string $file, int $line] )
>
> Just calls the constructor of the base class.
>
>
> How do I "throw" an error?
>
> All my classes that might "throw" an error have a public instance
> variables $err which holds the last exception object. Whenever an error
> occurs the method saves the exception in $this->err and tries to return
> some usefull value (false, empty array etc.) to preevent errors that
> appear because an unexpected datatype gets returned. Anyway this is not
> necessary for PEAR. Simply do: 'return MyPackageException("trouble
> somewhere", __FILE__, __LINE__);'.
>
> How do I "catch" an error?
>
> $ok = DB::connect();
> if ( is_object($ok) && "Exception" == substr(get_classname($ok), ??end
> of beginning?? ) ) {
> $msg = $ok->getLocalizedMessage();
> # $ok->printLocalizedMessage();
> }
>
Easier yet, have DB:: (or whatever class) provide a method that checks
whether or not it is an error object (we have something like that now):
$ok = DB::connect(..);
if (DB::isError ($ok)) {
/* .. */
}
-Sterling