private function handleInvocationException($exception, $activeHandler, $isCli)
    {
        $this->logger->debug(sprintf(
            "SnappyRouter: caught exception while invoking handler: %s (%d)",
            $exception->getMessage(),
            $exception->getCode()
        ));

        // if we have a valid handler give it a chance to handle the error
        if (null !== $activeHandler) {
            $activeHandler->invokePluginsHook(
                'errorOccurred',
                array($activeHandler, $exception)
            );
            return $activeHandler->getEncoder()->encode(
                new Response($activeHandler->handleException($exception))
            );
        }

        // if not on the command line, set an HTTP response code
        if (!$isCli) {
            $responseCode = AbstractResponse::RESPONSE_SERVER_ERROR;
            if ($exception instanceof RouterExceptionInterface) {
                $responseCode = $exception->getAssociatedStatusCode();
            }
            \Vectorface\SnappyRouter\http_response_code($responseCode);
        }
        return $exception->getMessage();
    }