Possible to Trigger Error from Listener?

Snog

Well-known member
Is it possible to trigger an error from within a listener? Either I'm missing something obvious or it isn't possible.

Code:
class My_Listner
{
      public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
      {
            if(somecondition)
            {
                  Do this
            }else{
                  Trigger error
            }
      }
}
 
Should be possible to throw an exception anywhere.

Though with it being a template hook I'm not sure how that would manifest itself.
 
I must be having a really bad day then. :(

return $this->responseError

results in a $this when not in object context error.
 
I must be having a really bad day then. :(

return $this->responseError

results in a $this when not in object context error.
You won't be able to use $this in that context.

Something like this should be ok: throw new XenForo_Exception();

I'm away from my computer at the moment so I can't remember the exact parameters but that should be a step in the right direction.
 
You can't use "$this" in a static method and the responseError method you're referring to lives in the XenForo_Controller class -- template hooks are static methods, and besides your Listener doesn't extend the XenForo_Controller class so it can't get to that function.

As I said, you would need to throw an exception, e.g.

PHP:
throw new XenForo_Exception('Error.', true);
 
I tried that a while ago. It results in an error being logged with the title of the error in the error log, and a php error being displayed, but doesn't throw the error on the intended page.
 
The view layer is really too late to be throwing a page-level error like that. You should be doing it sometime during controller execution (no later than post-dispatch).
 
I did think something like that would happen, hence:

Though with it being a template hook I'm not sure how that would manifest itself.

Are you sure there's nothing you could do to reapproach that? Due to the intended use of template hooks (and don't forget they are deprecated) it likely isn't fit for purpose.

Extending the controller in question, adding the params needed to the view response and extending the template with template modifications is likely going to be much better.
 
I thought it might be too late trying to use it in the listener.

I was trying to avoid extending the controller I'm working with. But I will have to re-think that approach.
 
Back
Top Bottom