Undocumented Exception-Handling Logic Hooks

sugarcrmdevelopers —  March 21, 2013

SugarCRM features lots of documented logic hooks, but this is the first time I’ve encountered these error/exception handling ones. I just happened to see them in some code I was browsing.

These hooks are apparently undocumented, and I can only think of a single use case for performing actions based on receiving a PHP Exception, and that would be to tie-in to a third-party logging or monitoring tool.

Still, cool to see in the system.

The code block is in include/MVC/Controller/SugarController.php. This handleException method is previously defined as

	/**
	 * This method is called from SugarApplication->execute and it will bootstrap the entire controller process
	 */
	final public function execute()
    {

        try
        {
            $this->process();
            if(!empty($this->view))
            {
                $this->processView();
            }
            elseif(!empty($this->redirect_url))
            {
            			$this->redirect();
            }
        }
        catch (Exception $e)
        {
            $this->handleException($e);
        }



	}

    /**
      * Handle exception
      * @param Exception $e
      */
    protected function handleException(Exception $e)
    {
        $GLOBALS['log']->fatal('Exception in Controller: ' . $e->getMessage());
        $logicHook = new LogicHook();

        if (isset($this->bean))
        {
            $logicHook->setBean($this->bean);
            $logicHook->call_custom_logic($this->bean->module_dir, "handle_exception", $e);
        }
        else
        {
            $logicHook->call_custom_logic('', "handle_exception", $e);
        }
    }