Tutorials: Error Handling

Setting If Errors Should Be Displayed

By default, when your site is created, framework takes decision in stderr.xml file to display errors:

<display_errors> <local>1</local> </display_errors>

For live DEVELOPMENT_ENVIRONMENT, however, you would not like to display errors to outside world, so change above to:

<display_errors> <local>1</local> <live>0</live> </display_errors>

Getting Exception Information

Framework collects via STDERR MVC API information about uncaught exception/error handled and matching <exception> route into a Lucinda\MVC\STDERR\Request object. Only this method is relevant for developers:

Setting Error Reporters

In order to log errors automatically, you first need to open stderr.xml and create a <reporter> tag (which will hold your reporter settings), child of DEVELOPMENT_ENVIRONMENT tag, child of <reporters> tag. Example:

<reporters> <local> <reporter class="FileReporter" path="C:\xampp\htdocs\errors" format="%d %v %u %e %f %l %m"/> ... </local> </reporters>

This configures reporting into an C:\xampp\htdocs\error.log file for local DEVELOPMENT_ENVIRONMENT. To learn more how to configure this tag, check official documentation or its framework integration!

Log line can be formatted via format attribute above based on a combination of following placeholders:

Writing New Error Reporters

Rarely, developers may need a solution for error reporting other than the one that comes with framework already, defined by Setting Error Reporters section. To do so, they must first register a new <reporter> in <reporters< tag @ stderr.xml. Example:

<reporters> <local> ... <reporter class="SQLReporter" table="errors"/> </local> ... </reporters>

Above is set to report errors in table errors via class SQLReporter that must implement Lucinda\MVC\STDERR\ErrorReporter and be located in application/reporters folder. Example:

class SQLReporter implements Lucinda\MVC\STDERR\ErrorReporter { private $tableName; public function __construct(\SimpleXMLElement $xml) { $this->tableName = (string) $xml["table"]; } public function report(Request $request) { $exception = $request->getException(); try { SQL("INSERT INTO ".$this->tableName." (message, file, line) VALUES (:message, :file, :line)", array(":message"=>$exception->getMessage(), ":file"=>$exception->getFile, ":line"=>$exception->getLine)); } catch(\Exception $e) {} } }

assuming following MySQL table structure:

CREATE TABLE errors ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, message TEXT NOT NULL, file VARCHAR(255) NOT NULL, line VARCHAR(255) NOT NULL, date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(id) )

To learn more about how STDERR error reporters work and what can be achieved with them, check their framework integration!


Share