Skip to content

Commit

Permalink
Strip HTML when receiving HTML server errors in cloud API
Browse files Browse the repository at this point in the history
It breaks the QField message log, as it supports HTML.
  • Loading branch information
suricactus committed Mar 21, 2024
1 parent 7afa1b9 commit 84a1691
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/core/qfieldcloudconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <QNetworkCookie>
#include <QNetworkCookieJar>
#include <QSettings>
#include <QTextDocument>
#include <QTextDocumentFragment>
#include <QTimer>
#include <QUrlQuery>
#include <qgsapplication.h>
Expand Down Expand Up @@ -507,29 +507,31 @@ QFieldCloudConnection::CloudError::CloudError( QNetworkReply *reply )
break;
}

errorMessage = QTextDocumentFragment::fromHtml( errorMessage ).toPlainText().trimmed();

QString httpErrorMessage = QStringLiteral( "[HTTP/%1] %2 " ).arg( mHttpCode ).arg( reply->url().toString() );
httpErrorMessage += ( mHttpCode >= 400 )
? tr( "Server Error." )
: tr( "Network Error." );
httpErrorMessage += mPayload.left( 200 );
httpErrorMessage = QTextDocumentFragment::fromHtml( httpErrorMessage ).toPlainText().trimmed();
QString payloadPlainText = QTextDocumentFragment::fromHtml( mPayload ).toPlainText().trimmed();

if ( mPayload.size() > 200 )
errorMessage += QStringLiteral( "" );

if ( errorMessage.isEmpty() )
{
errorMessage = httpErrorMessage;
QgsMessageLog::logMessage( QStringLiteral( "%1\n%2\n%3" ).arg( errorMessage, mPayload ).arg( reply->errorString() ) );
QgsMessageLog::logMessage( QStringLiteral( "%1\n%2\n%3" ).arg( errorMessage, payloadPlainText ).arg( reply->errorString() ) );
}
else
{
QgsMessageLog::logMessage( QStringLiteral( "%1\n%2\n%3\n%4" ).arg( errorMessage, httpErrorMessage, mPayload ).arg( reply->errorString() ) );
QgsMessageLog::logMessage( QStringLiteral( "%1\n%2\n%3\n%4" ).arg( errorMessage, httpErrorMessage, payloadPlainText ).arg( reply->errorString() ) );
}

// strip HTML tags
QTextDocument doc;
doc.setHtml( errorMessage );
errorMessage = doc.toPlainText();
errorMessage = QTextDocumentFragment::fromHtml( httpErrorMessage ).toPlainText();

mMessage = errorMessage;
}
Expand Down

0 comments on commit 84a1691

Please sign in to comment.