diff options
author | Marc Mutz <marc.mutz@qt.io> | 2024-01-10 08:57:43 +0100 |
---|---|---|
committer | Juha Vuolle <juha.vuolle@qt.io> | 2024-01-12 06:37:19 +0200 |
commit | 3a61de282c4740efe4a6fa1672e66efaf7c2b408 (patch) | |
tree | 06c11997fbcf776d5a9e3a4b1de541fc741d516a /src/network/access | |
parent | fbe29fb3684b02bec5f6019755d0a1bb0c94c275 (diff) |
QRestReply: optionally return the QJsonParseError from json()
... and remove the debug output of the internal QJsonParseError.
This allows users of the function to get the details in
machine-readable form, and to distinguish between !finished and an
actual Json parsing error.
Found in API-review.
Pick-to: 6.7
Change-Id: Ia237b192a894d692b965f6bedb4c94d3b6537535
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
Diffstat (limited to 'src/network/access')
-rw-r--r-- | src/network/access/qrestreply.cpp | 17 | ||||
-rw-r--r-- | src/network/access/qrestreply.h | 3 |
2 files changed, 12 insertions, 8 deletions
diff --git a/src/network/access/qrestreply.cpp b/src/network/access/qrestreply.cpp index 8c575733dc..e9334a873c 100644 --- a/src/network/access/qrestreply.cpp +++ b/src/network/access/qrestreply.cpp @@ -157,31 +157,34 @@ void QRestReply::abort() The returned value is wrapped in \c std::optional. If the conversion from the received data fails (empty data or JSON parsing error), - \c std::nullopt is returned. + \c std::nullopt is returned, and \a error is filled with details. Calling this function consumes the received data, and any further calls to get response data will return empty. This function returns \c {std::nullopt} and will not consume - any data if the reply is not finished. + any data if the reply is not finished. If \a error is passed, it will be + set to QJsonParseError::NoError to distinguish this case from an actual + error. \sa body(), text(), finished(), isFinished() */ -std::optional<QJsonDocument> QRestReply::json() +std::optional<QJsonDocument> QRestReply::json(QJsonParseError *error) { Q_D(QRestReply); if (!isFinished()) { qCWarning(lcQrest, "Attempt to read json() of an unfinished reply, ignoring."); + if (error) + *error = {0, QJsonParseError::ParseError::NoError}; return std::nullopt; } QJsonParseError parseError; const QByteArray data = d->networkReply->readAll(); const QJsonDocument doc = QJsonDocument::fromJson(data, &parseError); - if (parseError.error != QJsonParseError::NoError) { - qCDebug(lcQrest) << "Response data not JSON:" << parseError.errorString() - << "at" << parseError.offset << data; + if (error) + *error = parseError; + if (parseError.error) return std::nullopt; - } return doc; } diff --git a/src/network/access/qrestreply.h b/src/network/access/qrestreply.h index b4616e7fb3..9a99e9e64a 100644 --- a/src/network/access/qrestreply.h +++ b/src/network/access/qrestreply.h @@ -12,6 +12,7 @@ QT_BEGIN_NAMESPACE class QByteArray; class QDebug; +struct QJsonParseError; class QJsonDocument; class QString; @@ -25,7 +26,7 @@ public: QNetworkReply *networkReply() const; - std::optional<QJsonDocument> json(); + std::optional<QJsonDocument> json(QJsonParseError *error = nullptr); QByteArray body(); QString text(); |