开发者

sfErrorNotifierPlugin: The "default" context does not exist

I have installed the sfErrorNotifierPlugin. When both options reportErrors/reportPHPErrors reportPHPWarnings/reportWarnings are set to false, everything is ok. But I want to catch PHP exceptions and warnings to receive E-mails, but then all my tasks fail, including clear-cache. After few hours of tests I'm 100% sure that the problem is with set_exception_handler/set_error_handler.

There's a similar question: sfErrorNotifierPlugin on symfony task but the author 开发者_Python百科there is having problems with a custom task. In my case, even built-in tasks fail.


I haven't used sfErrorNotifierPlugin, but I have run into 'The “default” context does not exist.' messages before. It happens when a call is made to sfContext::getInstance() and the context simply doesn't exist. I've had this happen a lot from within custom tasks. One solution is to add sfContext::createInstance() before the call to sfContext::getInstance(). This will ensure that a context exists.

There's an interesting blog post on 'Why sfContext::getInstance() is bad' that goes into more detail - http://webmozarts.com/2009/07/01/why-sfcontextgetinstance-is-bad/


Well, the problem could not be solved this way, unfortunately. Using sfErrorNotifierPlugin, I have enabled reporting PHP warning/errors (apart from symfony exceptions) and this resulted in huge problems, e.g. built-in tasks such as clear-cache failed.

The solution I chose was to load the plugin only in non-task mode (project configuration class):

  public function setup()
  {
    $this->enableAllPluginsExcept('sfPropelPlugin');
    if ('cli' == php_sapi_name()) $this->disablePlugins('sfErrorNotifierPlugin');
  }

WHen a task is executed, everything works normally. When an app is fired from the browser, emails are sent when exception/warning occurs (maybe someone will find it useful).


Arms has explained the problem correctly. But usually context does not exist when executing backend/maintenance tasks on the console. And it is easier if you handle the condition yourself.

Check, if you really need the context?

If you do, what exactly do you need it for? Sometimes you only want a user to populate a created_by field. You can work around by hard-coding a user ID.

If you want to do something more integrated, create a page (which will have a context) and trigger the task from there.


you can test the existance of the instance before doing something inside a class. Like:

if(sfContext::hasInstance())
  $this->microsite_id = sfContext::getInstance()->getUser()->getAttribute('active_microsite');


I've been experiencing the same problem using the plugin sfErrorNotifier.

In my specific case, I noticed a warning was raised:

Warning: ob_start(): function '' not found or invalid function name in /var/www/ncsoft_qa/lib/vendor/symfony/lib/config/sfApplicationConfiguration.class.php on line 155

Notice: ob_start(): failed to create buffer in /var/www/ncsoft_qa/lib/vendor/symfony/lib/config/sfApplicationConfiguration.class.php on line 155

So, checking the file: sfApplicationConfiguration.class.php class, line 155,

I've replaced the ' ' for a null, then the warnings disappears, and also the error!

ob_start(sfConfig::get('sf_compressed') ? 'ob_gzhandler' : ''); bad

ob_start(sfConfig::get('sf_compressed') ? 'ob_gzhandler' : null); good

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜