Results 1 to 8 of 8

Thread: Handling / capture view exceptions

  1. #1
    gentlemich is offline Junior Member
    Join Date
    Jan 2009
    Posts
    19

    Angry Handling / capture view exceptions

    Hello everybody,
    I have perused through many forums and haven't found the answer to my question:

    How can I handle small exceptions appearing in the view of my Zend application?

    I am already using the default Zend error plugin and it is working perfectly to handle 404, 500 errors (database, etc.)

    But when I'm getting small errors (PHP notice, warning, parse error, etc.): for example when I'm displaying a variable in my view that doesn't exist => the error is displayed (which is not nice at all).
    Ex: <?=$abc?> where $abc does not exist.
    I want the program to capture it and send me an email.

    Anybody has an idea?
    Thanks,
    Mike
    Last edited by gentlemich; 05-07-2009 at 04:53 PM.

  2. #2
    Tekerson is offline Senior Member
    Join Date
    Jul 2008
    Posts
    288

    Default

    This isn't specific to ZF, but general to any PHP application.

    Firstly, you want to turn of display_errors in your productions environment. As you say, seeing them is not nice. You can instead log them to a log file on the server (PHP: Error Handling - Manual).
    Depending on how much control you have over the server, you could then set up a cron (or log rotation rule) to check the log and email it to you if it isn't empty.

    Another option is to use a custom error handler (PHP: set_error_handler - Manual), you can then email the error details to yourself as they happen. But your custom handler does have to correctly deal with the different error levels.

    Which is the best option would depend on factors such as server access and how many errors you are getting; if you're getting 100/minute you probably don't want to get a separate email for every one.
    Brenton Alker
    PHP Developer - Brisbane, Australia

    blog.tekerson.com | twitter.com/tekerson | brenton.mp

  3. #3
    Eugen is offline Senior Member
    Join Date
    Sep 2008
    Location
    Croatia
    Posts
    400

    Default

    Quote Originally Posted by Tekerson View Post
    Which is the best option would depend on factors such as server access and how many errors you are getting; if you're getting 100/minute you probably don't want to get a separate email for every one.
    Ye, you need to be careful about email thing, because if you will have high traffic site one little notice can generate 1000 emails per hour..
    So try to make something like a log file for every day and send it to yourself at the end of that day..

  4. #4
    gentlemich is offline Junior Member
    Join Date
    Jan 2009
    Posts
    19

    Default

    Thanks a lot!

    I now can log all the PHP errors in a file or send them via e-mail.

    There is still one problem: Zend doesn't catch the errors anymore. I.e. the 404, 500 errors I was talking about are not processed by the ErrorController.php

    What can I do to reinstore that?

    Thanks,
    Mike

  5. #5
    Tekerson is offline Senior Member
    Join Date
    Jul 2008
    Posts
    288

    Default

    It depends, how did you go about suppressing the errors?
    Brenton Alker
    PHP Developer - Brisbane, Australia

    blog.tekerson.com | twitter.com/tekerson | brenton.mp

  6. #6
    gentlemich is offline Junior Member
    Join Date
    Jan 2009
    Posts
    19

    Default

    Quote Originally Posted by Tekerson View Post
    It depends, how did you go about suppressing the errors?
    I'm using Zend's default Error Controller with different cases:
    [PHP]<?php
    /** Zend_Controller_Action */
    class ErrorController extends Zend_Controller_Action
    {
    private $_exception;
    private static $errorMessage;
    private static $httpCode;

    public function errorAction(){
    $this->_exception = $this->_getParam('error_handler');

    switch ($this->_exception->type) {
    case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ CONTROLLER:
    case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ ACTION:
    $this->getResponse()->setHttpResponseCode(404);
    self::$httpCode = 404;
    self::$errorMessage = $this->view->translate('Page not found');
    break;
    case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTH ER:
    switch (get_class($this->_exception->exception)) {
    case 'Zend_View_Exception' :
    $this->getResponse()->setHttpResponseCode(500);
    self::$httpCode = 500;
    self::$errorMessage = $this->view->translate('Processing error');
    break;
    case 'Zend_Db_Exception' :
    $this->getResponse()->setHttpResponseCode(503);
    self::$httpCode = 503;
    self::$errorMessage = $this->view->translate('Database error');
    break;
    case 'Metier_Exception' :
    $this->getResponse()->setHttpResponseCode(200);
    self::$httpCode = 200;
    self::$errorMessage = $this->_exception->exception->getMessage();
    break;
    default:
    $this->getResponse()->setHttpResponseCode(500);
    self::$httpCode = 500;
    self::$errorMessage = sprintf($this->view->translate('Unknown error: %1$s'), $this->_exception->exception->getMessage());
    break;
    }
    break;
    }

    $this->view->message = $msg = self::$errorMessage;
    $this->view->httpCode = $code = self::$httpCode;

    $this->render();

    $errors = $this->_getParam('error_handler');

    $ref = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:null;
    $host = isset($_SERVER["REMOTE_HOST"])?$_SERVER["REMOTE_HOST"]:null;
    if($code >= 500){
    mail('my mail', 'Error '.$code, $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].' '.$msg.'
    file:'.$this->_exception->exception->getFile().'
    line:'.$this->_exception->exception->getLine().'
    coming from:'.$ref.'
    user:'.$_SERVER["HTTP_USER_AGENT"].'
    user server:'.$host.'
    stack trace:'.Zend_Debug::dump($errors->exception,null,false));
    }
    }
    }

    [/PHP]

  7. #7
    Tekerson is offline Senior Member
    Join Date
    Jul 2008
    Posts
    288

    Default

    You need to make a distinction between php errors/warnings/notices and Exceptions. php errors are the "old" style errors you get in php, exceptions are the OO method of notifying the caller an exceptional (unexpected/unhandled) circumstance has taken place. Exceptions only become (fatal) errors if they are not caught.

    The ZF Error handler will only catch exceptions, it doesn't handle php errors. So setting you display_errors setting off should have no effect on the error handler being triggered. If the ZF error handler has stopped catching exceptions, there is something else going on; what happens when an exception is thrown now? Does it just go uncaught and trigger a fatal error, or do you get the infamous "white screen of death"?
    Brenton Alker
    PHP Developer - Brisbane, Australia

    blog.tekerson.com | twitter.com/tekerson | brenton.mp

  8. #8
    gentlemich is offline Junior Member
    Join Date
    Jan 2009
    Posts
    19

    Default

    OK sorry for the newbie errors: it is now working.
    I had $front->setParam('noErrorHandler', true)
    ->throwExceptions(true);
    in my bootstrap...

    Thanks a lot for the help.
    See you, Mike

Similar Threads

  1. Zend_Dojo_Form_Element_Editor does not capture input
    By AntiFish03 in forum Core Infrastructure
    Replies: 1
    Last Post: 07-26-2010, 05:08 AM
  2. Handling view scripts
    By MeroeKush in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 12-18-2009, 05:39 AM
  3. Fetch most Exceptions - how?
    By virus-2k in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 02-24-2009, 08:03 AM
  4. nice view of exceptions
    By The_Percival in forum Model-View-Controller (MVC)
    Replies: 3
    Last Post: 10-29-2008, 08:07 AM
  5. Replies: 0
    Last Post: 09-09-2008, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •