protected function handleForResponseException(
        ServerRequestInterface $request,
        ResponseInterface $response,
        \Throwable $e
    ) : ResponseInterface {
        if ($e instanceof MethodNotAllowedException) {
            $handler = $this->getNotAllowedHandler();
        } elseif ($e instanceof NotFoundException) {
            $handler = $this->getNotFoundHandler();
        } elseif ($e instanceof \Exception) {
            $handler = $this->getExceptionHandler();
        } else {
            $handler = $this->getErrorHandler();
        }

        $response = $handler($request, $response, $e);

        // check if buffer is zero and add the buffer
        if (ob_get_level() === 0) {
            $this->addCreateBuffer();
        }

        return $response;
    }