Skip to content

Commit d98e91d

Browse files
Add hresult wrapper (#210)
1 parent f08b0f3 commit d98e91d

14 files changed

Lines changed: 128 additions & 71 deletions

File tree

binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
],
3939
"include_dirs": [
4040
"include",
41-
"include/napi_helpers",
41+
"include/helpers",
4242
"include/placeholders_interface",
4343
"include/sync_root_interface",
4444
"include/sync_root_interface/callbacks",

dist/addon.node

2.5 KB
Binary file not shown.

include/helpers/check_hresult.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef WINRT_CHECK_HRESULT_WRAPPER_H
2+
#define WINRT_CHECK_HRESULT_WRAPPER_H
3+
4+
#include <winrt/base.h>
5+
#include <string>
6+
7+
inline void check_hresult(const char *key, HRESULT hr)
8+
{
9+
try
10+
{
11+
winrt::check_hresult(hr);
12+
}
13+
catch (const winrt::hresult_error &e)
14+
{
15+
std::string msg = std::string("[") + key + "] " + winrt::to_string(e.message());
16+
throw winrt::hresult_error(e.code(), winrt::to_hstring(msg));
17+
}
18+
}
19+
20+
#endif
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,52 @@
44
#include <string>
55
#include <tuple>
66

7-
template<typename T>
7+
template <typename T>
88
T napi_extract_value(napi_env env, napi_value value);
99

10-
template<>
11-
inline std::wstring napi_extract_value<std::wstring>(napi_env env, napi_value value) {
10+
template <>
11+
inline std::wstring napi_extract_value<std::wstring>(napi_env env, napi_value value)
12+
{
1213
size_t length;
1314
napi_get_value_string_utf16(env, value, nullptr, 0, &length);
1415

1516
std::wstring result(length + 1, L'\0');
1617
size_t actualLength;
17-
napi_get_value_string_utf16(env, value, reinterpret_cast<char16_t*>(result.data()), length + 1, &actualLength);
18+
napi_get_value_string_utf16(env, value, reinterpret_cast<char16_t *>(result.data()), length + 1, &actualLength);
1819
result.resize(actualLength);
1920

2021
return result;
2122
}
2223

23-
template<>
24-
inline int64_t napi_extract_value<int64_t>(napi_env env, napi_value value) {
24+
template <>
25+
inline int64_t napi_extract_value<int64_t>(napi_env env, napi_value value)
26+
{
2527
int64_t result;
2628
napi_get_value_int64(env, value, &result);
2729
return result;
2830
}
2931

30-
template<>
31-
inline bool napi_extract_value<bool>(napi_env env, napi_value value) {
32+
template <>
33+
inline bool napi_extract_value<bool>(napi_env env, napi_value value)
34+
{
3235
bool result;
3336
napi_get_value_bool(env, value, &result);
3437
return result;
3538
}
3639

37-
template<typename... Types, std::size_t... Is>
38-
inline std::tuple<Types...> napi_extract_args_impl(napi_env env, napi_value* argv, std::index_sequence<Is...>) {
40+
template <typename... Types, std::size_t... Is>
41+
inline std::tuple<Types...> napi_extract_args_impl(napi_env env, napi_value *argv, std::index_sequence<Is...>)
42+
{
3943
return std::make_tuple(napi_extract_value<Types>(env, argv[Is])...);
4044
}
4145

42-
template<typename... Types>
43-
inline std::tuple<Types...> napi_extract_args(napi_env env, napi_callback_info info) {
46+
template <typename... Types>
47+
inline std::tuple<Types...> napi_extract_args(napi_env env, napi_callback_info info)
48+
{
4449
constexpr size_t N = sizeof...(Types);
4550
size_t argc = N;
4651
napi_value argv[N];
4752
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
48-
53+
4954
return napi_extract_args_impl<Types...>(env, argv, std::make_index_sequence<N>{});
5055
}
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@
66
#include <string>
77

88
template <typename Fn>
9-
napi_value napi_safe_wrap(napi_env env, napi_callback_info info, Fn&& fn, const char* function_name) {
9+
napi_value napi_safe_wrap(napi_env env, napi_callback_info info, Fn &&fn, const char *function_name)
10+
{
1011
std::ostringstream oss;
1112

12-
try {
13+
try
14+
{
1315
return fn(env, info);
14-
} catch (const winrt::hresult_error& e) {
16+
}
17+
catch (const winrt::hresult_error &e)
18+
{
1519
oss << "[" << function_name << "] WinRT error: " << winrt::to_string(e.message()) << " (HRESULT: 0x" << std::hex << e.code() << ")";
16-
} catch (const std::exception& e) {
20+
}
21+
catch (const std::exception &e)
22+
{
1723
oss << "[" << function_name << "] " << e.what();
18-
} catch (...) {
24+
}
25+
catch (...)
26+
{
1927
oss << "[" << function_name << "] Unknown native error";
2028
}
21-
29+
2230
napi_throw_error(env, nullptr, oss.str().c_str());
2331
return nullptr;
2432
}

native-src/placeholders_interface/Planceholders.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <windows.h>
1515
#include <shlobj.h>
1616
#include "convert_to_placeholder.h"
17+
#include <check_hresult.h>
1718

1819
#pragma comment(lib, "shlwapi.lib")
1920

@@ -50,11 +51,13 @@ void Placeholders::UpdateSyncStatus(const std::wstring &path)
5051
{
5152
auto fileHandle = Placeholders::OpenFileHandle(path, FILE_WRITE_ATTRIBUTES, true);
5253

53-
winrt::check_hresult(CfSetInSyncState(
54-
fileHandle.get(),
55-
CF_IN_SYNC_STATE_IN_SYNC,
56-
CF_SET_IN_SYNC_FLAG_NONE,
57-
nullptr));
54+
check_hresult(
55+
"CfSetInSyncState",
56+
CfSetInSyncState(
57+
fileHandle.get(),
58+
CF_IN_SYNC_STATE_IN_SYNC,
59+
CF_SET_IN_SYNC_FLAG_NONE,
60+
nullptr));
5861

5962
SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, path.c_str(), nullptr);
6063
}
@@ -63,16 +66,18 @@ void Placeholders::UpdateFileIdentity(const std::wstring &path, const std::wstri
6366
{
6467
auto fileHandle = OpenFileHandle(path, FILE_WRITE_ATTRIBUTES, true);
6568

66-
winrt::check_hresult(CfUpdatePlaceholder(
67-
fileHandle.get(),
68-
nullptr,
69-
placeholderId.c_str(),
70-
static_cast<DWORD>(placeholderId.size() * sizeof(wchar_t)),
71-
nullptr,
72-
0,
73-
CF_UPDATE_FLAG_NONE,
74-
nullptr,
75-
nullptr));
69+
check_hresult(
70+
"CfUpdatePlaceholder",
71+
CfUpdatePlaceholder(
72+
fileHandle.get(),
73+
nullptr,
74+
placeholderId.c_str(),
75+
static_cast<DWORD>(placeholderId.size() * sizeof(wchar_t)),
76+
nullptr,
77+
0,
78+
CF_UPDATE_FLAG_NONE,
79+
nullptr,
80+
nullptr));
7681
}
7782

7883
FileState Placeholders::GetPlaceholderInfo(const std::wstring &path)
@@ -85,12 +90,14 @@ FileState Placeholders::GetPlaceholderInfo(const std::wstring &path)
8590
std::vector<char> buffer(infoSize);
8691
auto *info = reinterpret_cast<CF_PLACEHOLDER_BASIC_INFO *>(buffer.data());
8792

88-
winrt::check_hresult(CfGetPlaceholderInfo(
89-
fileHandle.get(),
90-
CF_PLACEHOLDER_INFO_BASIC,
91-
info,
92-
infoSize,
93-
nullptr));
93+
check_hresult(
94+
"CfGetPlaceholderInfo",
95+
CfGetPlaceholderInfo(
96+
fileHandle.get(),
97+
CF_PLACEHOLDER_INFO_BASIC,
98+
info,
99+
infoSize,
100+
nullptr));
94101

95102
std::string placeholderId(reinterpret_cast<const char *>(info->FileIdentity), info->FileIdentityLength);
96103

native-src/sync_root_interface/SyncRoot.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <filesystem>
55
#include <iostream>
66
#include <vector>
7+
#include <check_hresult.h>
78

89
std::map<std::wstring, CF_CONNECTION_KEY> connectionMap;
910

@@ -19,14 +20,14 @@ void SyncRoot::ConnectSyncRoot(const wchar_t *syncRootPath, InputSyncCallbacks s
1920

2021
CF_CONNECTION_KEY connectionKey;
2122

22-
HRESULT hr = CfConnectSyncRoot(
23-
syncRootPath,
24-
callbackTable,
25-
nullptr,
26-
CF_CONNECT_FLAG_REQUIRE_PROCESS_INFO | CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH,
27-
&connectionKey);
28-
29-
winrt::check_hresult(hr);
23+
check_hresult(
24+
"CfConnectSyncRoot",
25+
CfConnectSyncRoot(
26+
syncRootPath,
27+
callbackTable,
28+
nullptr,
29+
CF_CONNECT_FLAG_REQUIRE_PROCESS_INFO | CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH,
30+
&connectionKey));
3031

3132
connectionMap[syncRootPath] = connectionKey;
3233
}
@@ -36,9 +37,7 @@ void SyncRoot::DisconnectSyncRoot(const wchar_t *syncRootPath)
3637
auto it = connectionMap.find(syncRootPath);
3738
if (it != connectionMap.end())
3839
{
39-
HRESULT hr = CfDisconnectSyncRoot(it->second);
40-
41-
winrt::check_hresult(hr);
40+
check_hresult("CfDisconnectSyncRoot", CfDisconnectSyncRoot(it->second));
4241

4342
connectionMap.erase(it);
4443
}

native-src/sync_root_interface/callbacks/FetchData/FetchData.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <Callbacks.h>
22
#include <cfapi.h>
3+
#include <check_hresult.h>
34
#include <chrono>
45
#include <codecvt>
56
#include <condition_variable>
@@ -76,7 +77,7 @@ napi_value response_callback_fn_fetch_data(napi_env env, napi_callback_info info
7677
if (FAILED(hr))
7778
{
7879
transfer_data(ctx->connectionKey, ctx->transferKey, nullptr, ctx->requiredOffset, ctx->requiredLength, STATUS_UNSUCCESSFUL);
79-
winrt::throw_hresult(hr);
80+
check_hresult("transfer_data", hr);
8081
}
8182

8283
size_t completed = offset + length;
@@ -90,9 +91,12 @@ napi_value response_callback_fn_fetch_data(napi_env env, napi_callback_info info
9091
winrt::com_ptr<IShellItem2> shellItem;
9192
winrt::com_ptr<IPropertyStore> propStoreVolatile;
9293

93-
winrt::check_hresult(SHCreateItemFromParsingName(ctx->path.c_str(), nullptr, __uuidof(shellItem), shellItem.put_void()));
94+
check_hresult(
95+
"SHCreateItemFromParsingName",
96+
SHCreateItemFromParsingName(ctx->path.c_str(), nullptr, __uuidof(shellItem), shellItem.put_void()));
9497

95-
winrt::check_hresult(
98+
check_hresult(
99+
"shellItem->GetPropertyStore",
96100
shellItem->GetPropertyStore(
97101
GETPROPERTYSTOREFLAGS::GPS_READWRITE | GETPROPERTYSTOREFLAGS::GPS_VOLATILEPROPERTIESONLY,
98102
__uuidof(propStoreVolatile),

native-src/virtual_drive/convert_to_placeholder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "stdafx.h"
44
#include "napi_extract_args.h"
55
#include "Placeholders.h"
6+
#include <check_hresult.h>
67

78
void convert_to_placeholder(const std::wstring &path, const std::wstring &placeholderId)
89
{
@@ -19,7 +20,7 @@ void convert_to_placeholder(const std::wstring &path, const std::wstring &placeh
1920

2021
if (hr != 0x8007017C) // Already a placeholder
2122
{
22-
winrt::check_hresult(hr);
23+
check_hresult("CfConvertToPlaceholder", hr);
2324
}
2425
}
2526

native-src/virtual_drive/create_file_placeholder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Placeholders.h"
44
#include "convert_to_placeholder.h"
55
#include "napi_extract_args.h"
6+
#include <check_hresult.h>
67

78
napi_value create_file_placeholder_impl(napi_env env, napi_callback_info info)
89
{
@@ -33,7 +34,9 @@ napi_value create_file_placeholder_impl(napi_env env, napi_callback_info info)
3334
cloudEntry.FsMetadata.BasicInfo.LastAccessTime = lastAccessTime;
3435
cloudEntry.FsMetadata.BasicInfo.ChangeTime = lastWriteTime;
3536

36-
winrt::check_hresult(CfCreatePlaceholders(parentPath.c_str(), &cloudEntry, 1, CF_CREATE_FLAG_NONE, NULL));
37+
check_hresult(
38+
"CfCreatePlaceholders",
39+
CfCreatePlaceholders(parentPath.c_str(), &cloudEntry, 1, CF_CREATE_FLAG_NONE, NULL));
3740

3841
return nullptr;
3942
}

0 commit comments

Comments
 (0)