ED php framework :: Tutorial #11
How to deal with errors & The exception handling mechanism
The ED Framework has quite powerful way to handle errors through it's built-in exception handling mechanism.
    // On button click
    public function btnSubmit_OnClick() {
        TDataValidator::checkInt($this->ftPersonId->getValue(), "Person Id should be a number"); // throws TDataValidatorException
        TDataValidator::checkMoney($this->ftSalary->getValue(), "Salary must have money format like 10.00"); // throws TDataValidatorException

        $db = db();
        $db->insert("tbl_doesn't_exist", ...; // throws TDatabaseException
        $db->update("tbl_exists", ...;
    }
checkInt, checkMoney or the "insert" operation can throw exceptions. The TPage class, which all pages inherit provides default method to handle them.
    public function OnPageException($ex) {
        if (is_class($ex, "TDataValidatorException")) {
            return $this->showError($ex->getMessage());
        }
        else if (is_class($ex, "TDatabaseException")) {
            $this->showError("Database error. Check error.log");
        }
        else if (is_class($ex, "Exception")) {
            $this->showError($ex->getMessage());
        }

        errorLog($ex->getMessage());
        errorLog($ex->getTraceAsString());
    }
For most of the scenarios this method do the job, but you can always override it to better fit your needs.


How to log errors
The global "errorLog" function can take as argument any kind of object and will log it in "protected/log/error.log". Here comes the example:
    $db = db();
    $db->insert("tbl_doesn't_exist", ...;
    $db->update("tbl_exists", ...;

    if ($db->hasErrors()) {
        errorLog($db->getErrors());
        return;
    }


How to debug while developing your application
There are 2 global functions with the same name: "screen_debug". One defined on the the server side, one defined on the client side. Both take as argument any kind of object and will pring it on the page. "sd" is a shortcut function for "screen_debug".
    // On button click
    public function btnSubmit_OnClick() {
        screen_debug($this->ftPersonId->getValue());
        sd($this->ftSalary->getValue());
    }