1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Possible to Trigger Error from Listener?

Discussion in 'XenForo Development Discussions' started by Snog, Apr 15, 2015.

  1. Snog

    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.

    class My_Listner
          public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
                      Do this
                      Trigger error
  2. Chris D

    Chris D XenForo Developer Staff Member

    Should be possible to throw an exception anywhere.

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

    Snog Well-Known Member

    I must be having a really bad day then. :(

    return $this->responseError

    results in a $this when not in object context error.
  4. NixFifty

    NixFifty Well-Known Member

    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.
  5. Chris D

    Chris D XenForo Developer Staff Member

    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.

    throw new XenForo_Exception('Error.'true);
  6. Snog

    Snog Well-Known Member

    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.
  7. Mike

    Mike XenForo Developer Staff Member

    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).
  8. Chris D

    Chris D XenForo Developer Staff Member

    I did think something like that would happen, hence:

    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.
  9. Snog

    Snog Well-Known Member

    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.

Share This Page