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 route into a 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 tag (which will hold your reporter settings), child of DEVELOPMENT ENVIRONMENT tag, child of tag. Example:

<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. Each class attribute points to a class name in application/reporters that must extend . Framework comes with two implementations by default:

If you are using a binding as the two above, log lines can be formatted via format attribute above 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 tag @ stderr.xml. Example:

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

Above is set to report errors in table errors via class SQLReporter that must extend and be located in application/reporters folder. Example:

class SQLReporter extends { public function run(): void { $tableName = (string) $this->xml["table"]; $exception = $this->request->getException(); try { SQL("INSERT INTO ".$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) )
×