/**
  * If an SecurityException is being caught, ajax requests return a JSON error
  * response and non ajax requests redirect to the index
  * @param Controller $controller the controller that is being called
  * @param string $methodName the name of the method that will be called on
  *                           the controller
  * @param \Exception $exception the thrown exception
  * @throws \Exception the passed in exception if it cant handle it
  * @return Response a Response object or null in case that the exception could not be handled
  */
 public function afterException($controller, $methodName, \Exception $exception)
 {
     if ($exception instanceof SecurityException) {
         if (stripos($this->request->getHeader('Accept'), 'html') === false) {
             $response = new JSONResponse(array('message' => $exception->getMessage()), $exception->getCode());
             $this->app->log($exception->getMessage(), 'debug');
         } else {
             // TODO: replace with link to route
             $url = $this->app->getServer()->getURLGenerator()->getAbsoluteURL('index.php');
             $response = new RedirectResponse($url);
             $this->app->log($exception->getMessage(), 'debug');
         }
         return $response;
     }
     throw $exception;
 }