LSP: log non protocol lines instead of throwing an error

Some language server send non protocol conform lines over the same
transport layer as the protocol messages. Do not switch the client for
those servers into the error state, but print a warning message with the
content of these lines to a categorized log.

Change-Id: Ic6c62648f0237362136fd657fde71dd104bca9d1
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2019-05-08 12:08:39 +02:00
parent e0a1b31e29
commit ea81b676c7
3 changed files with 15 additions and 15 deletions

View File

@@ -37,6 +37,8 @@
namespace LanguageServerProtocol {
Q_LOGGING_CATEGORY(parseLog, "qtc.languageserverprotocol.parse", QtWarningMsg)
BaseMessage::BaseMessage()
: mimeType(JsonRpcMessageHandler::jsonRpcMimeType())
{ }
@@ -68,8 +70,7 @@ bool BaseMessage::operator==(const BaseMessage &other) const
return true;
}
static QPair<QByteArray, QByteArray> splitHeaderFieldLine(
const QByteArray &headerFieldLine, QString &parseError)
static QPair<QByteArray, QByteArray> splitHeaderFieldLine(const QByteArray &headerFieldLine)
{
static const int fieldSeparatorLength = int(std::strlen(headerFieldSeparator));
int assignmentIndex = headerFieldLine.indexOf(headerFieldSeparator);
@@ -77,8 +78,7 @@ static QPair<QByteArray, QByteArray> splitHeaderFieldLine(
return {headerFieldLine.mid(0, assignmentIndex),
headerFieldLine.mid(assignmentIndex + fieldSeparatorLength)};
}
parseError = BaseMessage::tr("Unexpected header line \"%1\".")
.arg(QLatin1String(headerFieldLine));
qCWarning(parseLog) << "Unexpected header line:" << QLatin1String(headerFieldLine);
return {};
}
@@ -134,8 +134,7 @@ void BaseMessage::parse(QBuffer *data, QString &parseError, BaseMessage &message
message.content = data->read(message.contentLength);
return;
}
const QPair<QByteArray, QByteArray> nameAndValue =
splitHeaderFieldLine(headerFieldLine, parseError);
const QPair<QByteArray, QByteArray> nameAndValue = splitHeaderFieldLine(headerFieldLine);
const QByteArray &headerFieldName = nameAndValue.first.trimmed();
const QByteArray &headerFieldValue = nameAndValue.second.trimmed();
@@ -146,9 +145,8 @@ void BaseMessage::parse(QBuffer *data, QString &parseError, BaseMessage &message
} else if (headerFieldName == contentTypeFieldName) {
parseContentType(message, headerFieldValue, parseError);
} else {
parseError = tr("Unexpected header field \"%1\" in \"%2\".")
.arg(QLatin1String(headerFieldName),
QLatin1String(headerFieldLine));
qCWarning(parseLog) << "Unexpected header field" << QLatin1String(headerFieldName)
<< "in" << QLatin1String(headerFieldLine);
}
}

View File

@@ -31,6 +31,7 @@
#include <QByteArray>
#include <QCoreApplication>
#include <QLoggingCategory>
QT_BEGIN_NAMESPACE
class QBuffer;
@@ -39,6 +40,8 @@ QT_END_NAMESPACE
namespace LanguageServerProtocol {
LANGUAGESERVERPROTOCOL_EXPORT Q_DECLARE_LOGGING_CATEGORY(parseLog)
class LANGUAGESERVERPROTOCOL_EXPORT BaseMessage
{
Q_DECLARE_TR_FUNCTIONS(BaseMessage)

View File

@@ -35,7 +35,6 @@
using namespace LanguageServerProtocol;
static Q_LOGGING_CATEGORY(LOGLSPCLIENTV, "qtc.languageclient.messages", QtWarningMsg);
static Q_LOGGING_CATEGORY(LOGLSPCLIENTPARSE, "qtc.languageclient.parse", QtWarningMsg);
namespace LanguageClient {
@@ -64,8 +63,8 @@ void BaseClientInterface::resetBuffer()
void BaseClientInterface::parseData(const QByteArray &data)
{
const qint64 preWritePosition = m_buffer.pos();
qCDebug(LOGLSPCLIENTPARSE) << "parse buffer pos: " << preWritePosition;
qCDebug(LOGLSPCLIENTPARSE) << " data: " << data;
qCDebug(parseLog) << "parse buffer pos: " << preWritePosition;
qCDebug(parseLog) << " data: " << data;
if (!m_buffer.atEnd())
m_buffer.seek(preWritePosition + m_buffer.bytesAvailable());
m_buffer.write(data);
@@ -73,9 +72,9 @@ void BaseClientInterface::parseData(const QByteArray &data)
while (!m_buffer.atEnd()) {
QString parseError;
BaseMessage::parse(&m_buffer, parseError, m_currentMessage);
qCDebug(LOGLSPCLIENTPARSE) << " complete: " << m_currentMessage.isComplete();
qCDebug(LOGLSPCLIENTPARSE) << " length: " << m_currentMessage.contentLength;
qCDebug(LOGLSPCLIENTPARSE) << " content: " << m_currentMessage.content;
qCDebug(parseLog) << " complete: " << m_currentMessage.isComplete();
qCDebug(parseLog) << " length: " << m_currentMessage.contentLength;
qCDebug(parseLog) << " content: " << m_currentMessage.content;
if (!parseError.isEmpty())
emit error(parseError);
if (!m_currentMessage.isComplete())