diff options
author | Hannah von Reth <hannah.vonreth@owncloud.com> | 2022-05-20 14:07:39 +0300 |
---|---|---|
committer | Hannah von Reth <vonreth@kde.org> | 2022-05-20 14:28:20 +0300 |
commit | a775767fefde42cf286906d7c59a1d6e1fb259f8 (patch) | |
tree | 9794121822e874f16264ec3bd8c37b2e49fce313 /shell_integration | |
parent | 2c2b34147f3ff436262eddda5471ffbfb44bf1f0 (diff) |
Improve error handling
Diffstat (limited to 'shell_integration')
4 files changed, 60 insertions, 29 deletions
diff --git a/shell_integration/windows/OCContextMenu/OCClientInterface.cpp b/shell_integration/windows/OCContextMenu/OCClientInterface.cpp index ee98f91d2..748b52482 100644 --- a/shell_integration/windows/OCContextMenu/OCClientInterface.cpp +++ b/shell_integration/windows/OCContextMenu/OCClientInterface.cpp @@ -15,6 +15,7 @@ #include "OCClientInterface.h" #include "CommunicationSocket.h" +#include "Log.h" #include "StringUtil.h" #include <shlobj.h> @@ -35,7 +36,6 @@ using std::min; #include <gdiplus.h> using namespace std; -#include <comdef.h> #include <wincrypt.h> #include <shlwapi.h> #include <wrl/client.h> @@ -48,21 +48,6 @@ using Microsoft::WRL::ComPtr; namespace { -template <typename T = wstring> -void log(const wstring &msg, const T &error = {}) -{ - wstringstream tmp; - tmp << L"ownCloud: " << msg; - if (!error.empty()) { - tmp << L" " << error.data(); - } - OutputDebugStringW(tmp.str().data()); -} -void logWinError(const wstring &msg, const DWORD &error = GetLastError()) -{ - log(msg, wstring(_com_error(error).ErrorMessage())); -} - bool sendV2(const CommunicationSocket &socket, const wstring &command, const nlohmann::json &j) { static int messageId = 0; @@ -91,19 +76,19 @@ std::shared_ptr<HBITMAP> saveImage(const string &data) std::vector<BYTE> buf(size, 0); DWORD skipped; if (!CryptStringToBinaryA(data.data(), 0, CRYPT_STRING_BASE64, buf.data(), &size, &skipped, nullptr)) { - logWinError(L"Failed to decode icon"); + OCShell::logWinError(L"Failed to decode icon"); return {}; } ComPtr<IStream> stream = SHCreateMemStream(buf.data(), size); if (!stream) { - log(L"Failed to create stream"); + OCShell::log(L"Failed to create stream"); return {}; }; HBITMAP result; Gdiplus::Bitmap bitmap(stream.Get(), true); const auto status = bitmap.GetHBITMAP(0, &result); if (status != Gdiplus::Ok) { - log(L"Failed to get HBITMAP", to_wstring(status)); + OCShell::log(L"Failed to get HBITMAP", to_wstring(status)); return {}; } return std::shared_ptr<HBITMAP> { new HBITMAP(result), [gdiplusToken](auto o) { @@ -143,7 +128,7 @@ OCClientInterface::ContextMenuInfo OCClientInterface::FetchInfo(const std::wstri const auto &arguments = msg.second["arguments"]; if (msg.first == L"V2/GET_CLIENT_ICON_RESULT") { if (arguments.contains("error")) { - log(L"V2/GET_CLIENT_ICON failed", arguments["error"].get<string>()); + OCShell::log(L"V2/GET_CLIENT_ICON failed", arguments["error"].get<string>()); } else { info.icon = saveImage(arguments["png"].get<string>()); } diff --git a/shell_integration/windows/OCUtil/CMakeLists.txt b/shell_integration/windows/OCUtil/CMakeLists.txt index 13a9f6f53..6fe100848 100644 --- a/shell_integration/windows/OCUtil/CMakeLists.txt +++ b/shell_integration/windows/OCUtil/CMakeLists.txt @@ -3,7 +3,7 @@ add_library(OCUtil STATIC RemotePathChecker.cpp StringUtil.cpp OCUtil.rc -) + ) target_include_directories(OCUtil PUBLIC diff --git a/shell_integration/windows/OCUtil/CommunicationSocket.cpp b/shell_integration/windows/OCUtil/CommunicationSocket.cpp index 244f8dce1..a023c616c 100644 --- a/shell_integration/windows/OCUtil/CommunicationSocket.cpp +++ b/shell_integration/windows/OCUtil/CommunicationSocket.cpp @@ -13,8 +13,9 @@ */ #include "CommunicationSocket.h" -#include "UtilConstants.h" +#include "Log.h" #include "StringUtil.h" +#include "UtilConstants.h" #include <iostream> #include <vector> @@ -89,13 +90,19 @@ bool CommunicationSocket::SendMsg(const wstring &message) const DWORD numBytesWritten = 0; - bool result = WriteFile(_pipe, utf8_msg.c_str(), static_cast<DWORD>(utf8_msg.size()), &numBytesWritten, &_overlapped); - - if (!result && GetLastError() == ERROR_IO_PENDING) { - WaitForSingleObject(_overlapped.hEvent, timeoutC); - result = GetOverlappedResult(_pipe, &_overlapped, &numBytesWritten, FALSE); + if (!WriteFile(_pipe, utf8_msg.c_str(), static_cast<DWORD>(utf8_msg.size()), &numBytesWritten, &_overlapped)) { + if (GetLastError() == ERROR_IO_PENDING) { + if (WaitForSingleObject(_overlapped.hEvent, timeoutC) != WAIT_OBJECT_0) { + OCShell::logWinError(L"SendMsg timed out"); + return false; + } + if (!GetOverlappedResult(_pipe, &_overlapped, &numBytesWritten, FALSE)) { + OCShell::logWinError(L"GetOverlappedResult failed"); + return false; + } + } } - return result; + return true; } bool CommunicationSocket::ReadLine(wstring *response) const @@ -129,8 +136,11 @@ bool CommunicationSocket::ReadLine(wstring *response) const if (!ReadFile(_pipe, resp_utf8.data(), DWORD(resp_utf8.size()), &numBytesRead, &_overlapped)) { if (GetLastError() == ERROR_IO_PENDING) { - WaitForSingleObject(_overlapped.hEvent, timeoutC); + if (WaitForSingleObject(_overlapped.hEvent, timeoutC) != WAIT_OBJECT_0) { + OCShell::logWinError(L"ReadLine timed out"); + } if (!GetOverlappedResult(_pipe, &_overlapped, &numBytesRead, FALSE)) { + OCShell::logWinError(L"GetOverlappedResult failed"); return false; } } else { diff --git a/shell_integration/windows/OCUtil/Log.h b/shell_integration/windows/OCUtil/Log.h new file mode 100644 index 000000000..f08974dbe --- /dev/null +++ b/shell_integration/windows/OCUtil/Log.h @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2022 Hannah von Reth <hannah.vonreth@owncloud.com>. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +#pragma once +#include <comdef.h> +#include <sstream> + +namespace OCShell { + +template <typename T = std::wstring> +void log(const std::wstring &msg, const T &error = {}) +{ + std::wstringstream tmp; + tmp << L"ownCloud ShellExtension: " << msg; + if (!error.empty()) { + tmp << L" " << error.data(); + } + OutputDebugStringW(tmp.str().data()); +} +inline void logWinError(const std::wstring &msg, const DWORD error = GetLastError()) +{ + log(msg, std::wstring(_com_error(error).ErrorMessage())); +} + +}
\ No newline at end of file |