TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Best way to handle errors
  • Hello!

    What is the best way to handle errors (ORM validation exceptions, Database exceptions and all other)? Every time i must use a try catch block to catch them (for example in models, create, update / save or delete), or simple catch in the index.php file?

    Thanks!

    mobal,

  • It depends on what you want to do when one of these Exceptions are thrown. Generally, catch them locally and perhaps rethrow them as HTTP exceptions.

    You don't need to catch in index.php, by the way: just override HTTP_Exception::get_response() and do whatever display and logging stuff that you need to do.

  • Sometimes i want to convert exceptions into error messages, or simple display custom error pages like 404, but every time i want to send an email about it.

    So when i locally handle an error, every time i must copy the email sender code block, and i want to do this automatically (this is the main goal).

  • You can do all that by overriding HTTP_Exception::get_response(). In that, you could email the error, and output whatever is appropriate.

    For example, if you have a database exception, you should catch it and re-throw, e.g.:

    $table = 'a_table';
    try {
        DB::insert($table)
          ->columns(array_keys($values))
          ->values($values)
          ->execute();
    } catch (Database_Exception $e) {
        throw new HTTP_Exception_500("Could not create a new :table record.", array(':table'=>$table), $e);
    }
    

    And then when you're handling the 500 exceptions, you can email etc.

  • Yes, but i wrote sometimes i need to convert exception into an error message. For example ORM update failed, i don't want to render an error page for the user, just redirect back and popup a message "Update failed. Error..."

    But now i answered my question. It is not the best way to handle all exceptions in the index.php file.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion