Skip to content

Commit 048c5c6

Browse files
authored
Accommodate other "key not found" errors (#687)
1 parent 5a5e33c commit 048c5c6

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

strings/base_error.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ namespace winrt::impl
584584
{
585585
inline hresult check_hresult_allow_bounds(hresult const result)
586586
{
587-
if (result != impl::error_out_of_bounds)
587+
if (result != impl::error_out_of_bounds && result != impl::error_fail && result != impl::error_file_not_found)
588588
{
589589
check_hresult(result);
590590
}

strings/base_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,5 @@ namespace winrt::impl
147147
constexpr hresult error_canceled{ static_cast<hresult>(0x800704C7) }; // HRESULT_FROM_WIN32(ERROR_CANCELLED)
148148
constexpr hresult error_bad_alloc{ static_cast<hresult>(0x8007000E) }; // E_OUTOFMEMORY
149149
constexpr hresult error_not_initialized{ static_cast<hresult>(0x800401F0) }; // CO_E_NOTINITIALIZED
150+
constexpr hresult error_file_not_found{ static_cast<hresult>(0x80070002) }; // HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)
150151
}

test/old_tests/UnitTests/TryLookup.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,37 @@ TEST_CASE("TryRemove")
110110

111111
TEST_CASE("TryLookup TryRemove error")
112112
{
113-
// Simulate a non-agile map that is being accessed from the wrong thread.
114-
// "Try" operations should throw rather than erroneously report "not found".
115-
// Because they didn't even try. The operation never got off the ground.
116-
struct incorrectly_used_non_agile_map : implements<incorrectly_used_non_agile_map, IMap<int, int>>
113+
// A map that throws a specific error, used to verify various edge cases.
114+
struct error_map : implements<error_map, IMap<int, int>>
117115
{
118-
int Lookup(int) { throw hresult_wrong_thread(); }
119-
int32_t Size() { throw hresult_wrong_thread(); }
120-
bool HasKey(int) { throw hresult_wrong_thread(); }
121-
IMapView<int, int> GetView() { throw hresult_wrong_thread(); }
122-
bool Insert(int, int) { throw hresult_wrong_thread(); }
123-
void Remove(int) { throw hresult_wrong_thread(); }
124-
void Clear() { throw hresult_wrong_thread(); }
116+
hresult code;
117+
int Lookup(int) { throw_hresult(code); }
118+
int32_t Size() { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test
119+
bool HasKey(int) { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test
120+
IMapView<int, int> GetView() { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test
121+
bool Insert(int, int) { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test
122+
void Remove(int) { throw_hresult(code); }
123+
void Clear() { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test
125124
};
126125

127-
auto map = make<incorrectly_used_non_agile_map>();
126+
auto self = make_self<error_map>();
127+
IMap<int, int> map = *self;
128+
129+
// Simulate a non-agile map that is being accessed from the wrong thread.
130+
// "Try" operations should throw rather than erroneously report "not found".
131+
// Because they didn't even try. The operation never got off the ground.
132+
self->code = RPC_E_WRONG_THREAD;
128133
REQUIRE_THROWS_AS(map.TryLookup(123), hresult_wrong_thread);
129134
REQUIRE_THROWS_AS(map.TryRemove(123), hresult_wrong_thread);
135+
136+
// Some implementations return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)
137+
// or E_FAIL when the key is not present.
138+
self->code = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
139+
REQUIRE(!map.TryLookup(123));
140+
REQUIRE(!map.TryRemove(123));
141+
142+
self->code = E_FAIL;
143+
REQUIRE(!map.TryLookup(123));
144+
REQUIRE(!map.TryRemove(123));
145+
130146
}

0 commit comments

Comments
 (0)