private function buildLayoutView(string $context = ViewInterface::CONTEXT_DEFAULT)
    {
        $currentRequest = $this->requestStack->getCurrentRequest();
        $masterRequest = $this->requestStack->getMasterRequest();

        if (!$currentRequest instanceof Request || !$masterRequest instanceof Request) {
            return null;
        }

        if ($masterRequest->attributes->has('ngbmExceptionLayoutView')) {
            // After an exception layout is resolved, this case either means that
            // the main layout does not exist at all (because the error
            // happened before the rendering) or that it is already resolved
            // (if the error happened in subrequest), so this is a subsequent
            // call where we can safely return null in all cases.
            return null;
        }

        if (
            !$currentRequest->attributes->has('exception') &&
            $masterRequest->attributes->has('ngbmLayoutView')
        ) {
            // This is the case where we request the main layout more than once
            // within the regular page display, without the exception, so again
            // we return null.
            return null;
        }

        // Once we're here, we either request the main layout when there's no
        // exception, or the exception layout (both of those for the first time).

        $layoutView = false;

        $resolvedRule = $this->layoutResolver->resolveRule();
        if ($resolvedRule instanceof Rule) {
            $layoutView = $this->viewBuilder->buildView(
                $resolvedRule->getLayout(),
                $context,
                [
                    'rule' => $resolvedRule,
                ]
            );
        }

        $masterRequest->attributes->set(
            $currentRequest->attributes->has('exception') ?
                'ngbmExceptionLayoutView' :
                'ngbmLayoutView',
            $layoutView
        );

        return $layoutView;
    }