protected function collectErrors()
    {
        if ($this->useInternalErrors === null) {
            throw new RuntimeException('Error recording not started');
        }

        $xmlErrors = libxml_get_errors();
        $errors = [];
        foreach ($xmlErrors as $error) {
            $errors[] = $this->formatLibXmlError($error);
        }
        libxml_clear_errors();
        libxml_use_internal_errors($this->useInternalErrors);
        $this->useInternalErrors = null;

        return $errors;
    }