Organization of technical and user error messages in php

I had a small problem with organizing error messages for two interacting classes. In one object, there are states that are β€œerrors,” where something went wrong or happened unexpectedly, but the situation still persists. I do not want to use exceptions, 1. because they have only one line for the message, and 2. because I want to access the object after an error. At least I want to use some of the get () methods to create a useful error message after an exception!

Ultimately, I have two messages that I want to report: one for me as an encoder, that something went wrong. This line will contain the technical data of the file, line, function / method, arguments and results. Obviously, I don’t want to show this to the user, so there is another line of error message that I want to show to the user ("This email address was not found").

Therefore, it occurs to me to create an array of error messages that can use the error code from the exceptions or the status code as keys for various messages. (Although, if I do, where can I store an array of messages?) Another option would be to create an error state object.

Is there something like "error patterns" similar to design patterns?

+4
source share
2 answers

Exceptions are really the best option; they do everything you requested. Even a few messages are possible, since exceptions are just classes that you can extend. You can simply pass an object that throws an exception for the specified exception.

<?php class ExampleException extends Exception { private $secondMessage; private $objectThatCausedIt; public function __construct( $secondMessage, $objectThatCausedIt ) { parent::__construct( "Descriptive message for developer", // error code for this type of error 1000 ); $this->secondMessage = $secondMessage; $this->objectThatCausedIt = $objectThatCausedIt; } public function getSecondMessage() { return $this->secondMessage; } public function getObjectThatCausedIt() { return $this->objectThatCausedIt; } } class Example { public function causeException() { throw new ExampleException( "Second Message", $this ); } } 

Now you just use the class and end the call, which may throw an exception in the try-catch block.

 <?php $example = new Example(); try { $example->causeException(); } catch ( ExampleException $e ) { // kind of pointless here, just an illustration // get object that caused it and do something with it. dump_and_log_function( $e->getObjectThatCausedIt() ); // or just use $example, which is still "alive" // and references the same instance dump_and_log_function( $example ); } 

Extending an exception has the advantage that you also get back stack traces. Backtrace contains information on how an exception occurred in which file, line and function. You can also access the error code, message, etc. I suggest you read the PHP documentation on exceptions ( http://php.net/manual/en/class.exception.php ) for more information.

Regarding error messages and logging, I usually use one class for this. The singleton class has only one instance of itself (if you did not know this). Here is a very simple example:

 <?php class Log { private $logFile; private static $instance; /* Log instances may only be constructed in Log::getInstance */ private function __construct() { $this->logFile = fopen( "path/to/log/file", "a" ); } public logMessage( $message ) { fwrite( $this->logFile, $message ); } public static getInstance() { if ( !self::$instance ) self::$instance = new self(); return self::$instance; } } 

Now, returning to the block of browns with exception handling, you can change it to something like this:

 <?php $example = new Example(); try { $example->causeException(); } catch ( ExampleException $e ) { // log developer message and backtrace Log::getInstance()->logMessage( $e->getMessage() ); Log::getInstance()->logMessage( $e->getTraceAsString() ); // or more compact by casting to string Log::getInstance()->logMessage( (string)$e ); // and now print error for users echo "<p>An Error has occured: {$e->getSecondMessage()}</p>"; } 

Instead of immediately responding to the error message, you can force the log class to have a property with an array of messages. You can then list them later in the script view all at once. You can also force the logMessage method to store messages in the session so that they can be displayed after the update (just remember to clear the messages from the session or they will be displayed again and again ;-).

+7
source

I do not want to use exceptions, 1. because they have only one line for the message, and 2. because I want to access the object after an error. At least I want to use some get () methods to build a useful error message after an exception!

You can create your own exception classes, and you can extend the PHP exception classes by adding the necessary support. However, you may have two settings, a developer and a client, where client errors go to display and developer errors rather than go to a log file or something like that.

Which translates into two custom exception types (although you can have a lot more, I say two different base classes).

0
source

All Articles