public function generateResponseFromModuleResult(ezpKernelResult $result)
    {
        $moduleResult = $result->getAttribute('module_result');

        if (isset($this->legacyLayout) && !$this->legacyMode && !$this->legacyResultHasLayout($result)) {
            // Replace original module_result content by filtered one
            $moduleResult['content'] = $result->getContent();

            $response = $this->render(
                $this->legacyLayout,
                ['module_result' => $moduleResult]
            );

            $response->setModuleResult($moduleResult);
        } else {
            $response = new LegacyResponse($result->getContent());
        }

        // Handling error codes sent by the legacy stack
        if (isset($moduleResult['errorCode'])) {
            if (!$this->legacyMode) {
                // If having an "Unauthorized" or "Forbidden" error code in non-legacy mode,
                // we send an AccessDeniedException to be able to trigger redirection to login in Symfony stack.
                if ($moduleResult['errorCode'] == 401 || $moduleResult['errorCode'] == 403) {
                    $errorMessage = isset($moduleResult['errorMessage']) ? $moduleResult['errorMessage'] : 'Access denied';
                    throw new AccessDeniedException($errorMessage);
                }

                // If having an "Not found" error code in non-legacy mode,
                // we send an NotFoundHttpException to be able to trigger error page in Symfony stack.
                if ($moduleResult['errorCode'] == 404) {
                    $errorMessage = isset($moduleResult['errorMessage']) ? $moduleResult['errorMessage'] : 'Not found';
                    throw new NotFoundHttpException($errorMessage, $response);
                }
            }

            $response->setStatusCode(
                $moduleResult['errorCode'],
                isset($moduleResult['errorMessage']) ? $moduleResult['errorMessage'] : null
            );
        }

        return $response;
    }