From a7d1a6f313b1ee73b8bd360a7dfb3f0fc56e64bf Mon Sep 17 00:00:00 2001 From: Sarper Akdemir Date: Fri, 21 Jun 2024 12:39:50 +0200 Subject: [PATCH] languagetool show cURL errors with the interaction handler Only shows the error once for a given checkerURL. Change-Id: I1873dc09584df33162c93d9d133b12eb8dfede04 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/169518 Reviewed-by: Michael Stahl Tested-by: Jenkins Reviewed-by: Sarper Akdemir --- lingucomponent/Library_LanguageTool.mk | 1 + .../languagetool/languagetoolimp.cxx | 76 +++++++++++++++---- .../languagetool/languagetoolimp.hxx | 5 +- 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/lingucomponent/Library_LanguageTool.mk b/lingucomponent/Library_LanguageTool.mk index 91907fe2b52f..aa09327cdec3 100644 --- a/lingucomponent/Library_LanguageTool.mk +++ b/lingucomponent/Library_LanguageTool.mk @@ -23,6 +23,7 @@ $(eval $(call gb_Library_use_libraries,LanguageTool,\ comphelper \ cppu \ cppuhelper \ + fwk \ i18nlangtag \ svt \ lng \ diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx index e50e96e4fb05..e565b3f269fe 100644 --- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx +++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.cxx @@ -47,8 +47,13 @@ #include #include #include +#include +#include +#include #include +#include #include +#include #include #include #include @@ -121,7 +126,7 @@ enum class HTTP_METHOD std::string makeHttpRequest_impl(std::u16string_view aURL, HTTP_METHOD method, const OString& aPostData, curl_slist* pHttpHeader, - tools::Long& nStatusCode) + tools::Long& nStatusCode, CURLcode& eCURLCode) { struct curl_cleanup_t { @@ -160,11 +165,12 @@ std::string makeHttpRequest_impl(std::u16string_view aURL, HTTP_METHOD method, (void)curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, aPostData.getStr()); } - CURLcode cc = curl_easy_perform(curl.get()); - if (cc != CURLE_OK) + eCURLCode = curl_easy_perform(curl.get()); + if (eCURLCode != CURLE_OK) { SAL_WARN("languagetool", - "CURL request returned with error: " << static_cast(cc)); + "CURL request returned with error: " << static_cast(eCURLCode) << " " + << curl_easy_strerror(eCURLCode)); } curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &nStatusCode); @@ -172,7 +178,7 @@ std::string makeHttpRequest_impl(std::u16string_view aURL, HTTP_METHOD method, } std::string makeDudenHttpRequest(std::u16string_view aURL, const OString& aPostData, - tools::Long& nStatusCode) + tools::Long& nStatusCode, CURLcode& eCURLCode) { struct curl_slist* pList = nullptr; OString sAccessToken @@ -186,11 +192,12 @@ std::string makeDudenHttpRequest(std::u16string_view aURL, const OString& aPostD pList = curl_slist_append(pList, sAccessToken.getStr()); } - return makeHttpRequest_impl(aURL, HTTP_METHOD::HTTP_POST, aPostData, pList, nStatusCode); + return makeHttpRequest_impl(aURL, HTTP_METHOD::HTTP_POST, aPostData, pList, nStatusCode, + eCURLCode); } std::string makeHttpRequest(std::u16string_view aURL, HTTP_METHOD method, const OString& aPostData, - tools::Long& nStatusCode) + tools::Long& nStatusCode, CURLcode& eCURLCode) { OString realPostData(aPostData); if (method == HTTP_METHOD::HTTP_POST) @@ -202,7 +209,7 @@ std::string makeHttpRequest(std::u16string_view aURL, HTTP_METHOD method, const realPostData += "&username=" + encodeTextForLT(username) + "&apiKey=" + apiKey; } - return makeHttpRequest_impl(aURL, method, realPostData, nullptr, nStatusCode); + return makeHttpRequest_impl(aURL, method, realPostData, nullptr, nStatusCode, eCURLCode); } template @@ -301,10 +308,41 @@ OUString getCheckerURL() return *oURL + "/check"; return {}; } + +void lclShowCURLErrorInteraction(const css::uno::Reference& xContext, + CURLcode eCURLCode, const OUString& rServer) +{ + if (!xContext.is()) + return; + + css::uno::Reference xInteractionHandler + = task::InteractionHandler::createWithParent(xContext, nullptr); + if (!xInteractionHandler.is()) + return; + + css::uno::Any aInteraction; + + rtl::Reference pApprove + = new comphelper::OInteractionApprove(); + css::uno::Sequence> aContinuations{ + pApprove + }; + + ucb::InteractiveNetworkConnectException aException( + { "(" + OUString::number(eCURLCode) + ") " + + OStringToOUString(curl_easy_strerror(eCURLCode), RTL_TEXTENCODING_UTF8) }, + {}, task::InteractionClassification_ERROR, rServer); + + aInteraction <<= aException; + xInteractionHandler->handle( + framework::InteractionRequest::CreateRequest(aInteraction, aContinuations)); +} } -LanguageToolGrammarChecker::LanguageToolGrammarChecker() +LanguageToolGrammarChecker::LanguageToolGrammarChecker( + const css::uno::Reference& xContext) : mCachedResults(10) + , mxContext(xContext) { } @@ -452,10 +490,22 @@ ProofreadingResult SAL_CALL LanguageToolGrammarChecker::doProofreading( tools::Long http_code = 0; std::string response_body; + CURLcode eCURLCode = CURLE_OK; if (bDudenProtocol) - response_body = makeDudenHttpRequest(checkerURL, postData, http_code); + response_body = makeDudenHttpRequest(checkerURL, postData, http_code, eCURLCode); else - response_body = makeHttpRequest(checkerURL, HTTP_METHOD::HTTP_POST, postData, http_code); + response_body + = makeHttpRequest(checkerURL, HTTP_METHOD::HTTP_POST, postData, http_code, eCURLCode); + + if (eCURLCode != CURLE_OK) + { + // show the cURL error only once for a given checkerURL + if (maLastErrorCheckerURL != checkerURL) + { + maLastErrorCheckerURL = checkerURL; + lclShowCURLErrorInteraction(mxContext, eCURLCode, checkerURL); + } + } if (http_code != 200) { @@ -512,9 +562,9 @@ void SAL_CALL LanguageToolGrammarChecker::initialize(const uno::Sequence const&) + css::uno::XComponentContext* pContext, css::uno::Sequence const&) { - return cppu::acquire(new LanguageToolGrammarChecker()); + return cppu::acquire(new LanguageToolGrammarChecker(pContext)); } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx index 93d2c84c612d..487d6d154e4b 100644 --- a/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx +++ b/lingucomponent/source/spellcheck/languagetool/languagetoolimp.hxx @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -37,11 +38,13 @@ class LanguageToolGrammarChecker css::uno::Sequence m_aSuppLocales; o3tl::lru_map> mCachedResults; + css::uno::Reference mxContext; + OUString maLastErrorCheckerURL; LanguageToolGrammarChecker(const LanguageToolGrammarChecker&) = delete; LanguageToolGrammarChecker& operator=(const LanguageToolGrammarChecker&) = delete; public: - LanguageToolGrammarChecker(); + LanguageToolGrammarChecker(const css::uno::Reference& xContext); virtual ~LanguageToolGrammarChecker() override; // XSupportedLocales