diff --git a/strings/base_error.h b/strings/base_error.h index 630e1dd33..f22214718 100644 --- a/strings/base_error.h +++ b/strings/base_error.h @@ -584,7 +584,7 @@ namespace winrt::impl { inline hresult check_hresult_allow_bounds(hresult const result) { - if (result != impl::error_out_of_bounds) + if (result != impl::error_out_of_bounds && result != impl::error_fail && result != impl::error_file_not_found) { check_hresult(result); } diff --git a/strings/base_types.h b/strings/base_types.h index 0eadc03cc..e6ca11fb0 100644 --- a/strings/base_types.h +++ b/strings/base_types.h @@ -147,4 +147,5 @@ namespace winrt::impl constexpr hresult error_canceled{ static_cast(0x800704C7) }; // HRESULT_FROM_WIN32(ERROR_CANCELLED) constexpr hresult error_bad_alloc{ static_cast(0x8007000E) }; // E_OUTOFMEMORY constexpr hresult error_not_initialized{ static_cast(0x800401F0) }; // CO_E_NOTINITIALIZED + constexpr hresult error_file_not_found{ static_cast(0x80070002) }; // HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) } diff --git a/test/old_tests/UnitTests/TryLookup.cpp b/test/old_tests/UnitTests/TryLookup.cpp index 47d2b5536..3c6c54580 100644 --- a/test/old_tests/UnitTests/TryLookup.cpp +++ b/test/old_tests/UnitTests/TryLookup.cpp @@ -110,21 +110,37 @@ TEST_CASE("TryRemove") TEST_CASE("TryLookup TryRemove error") { - // Simulate a non-agile map that is being accessed from the wrong thread. - // "Try" operations should throw rather than erroneously report "not found". - // Because they didn't even try. The operation never got off the ground. - struct incorrectly_used_non_agile_map : implements> + // A map that throws a specific error, used to verify various edge cases. + struct error_map : implements> { - int Lookup(int) { throw hresult_wrong_thread(); } - int32_t Size() { throw hresult_wrong_thread(); } - bool HasKey(int) { throw hresult_wrong_thread(); } - IMapView GetView() { throw hresult_wrong_thread(); } - bool Insert(int, int) { throw hresult_wrong_thread(); } - void Remove(int) { throw hresult_wrong_thread(); } - void Clear() { throw hresult_wrong_thread(); } + hresult code; + int Lookup(int) { throw_hresult(code); } + int32_t Size() { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test + bool HasKey(int) { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test + IMapView GetView() { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test + bool Insert(int, int) { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test + void Remove(int) { throw_hresult(code); } + void Clear() { throw_hresult(E_UNEXPECTED); } // shouldn't be called by the test }; - auto map = make(); + auto self = make_self(); + IMap map = *self; + + // Simulate a non-agile map that is being accessed from the wrong thread. + // "Try" operations should throw rather than erroneously report "not found". + // Because they didn't even try. The operation never got off the ground. + self->code = RPC_E_WRONG_THREAD; REQUIRE_THROWS_AS(map.TryLookup(123), hresult_wrong_thread); REQUIRE_THROWS_AS(map.TryRemove(123), hresult_wrong_thread); + + // Some implementations return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) + // or E_FAIL when the key is not present. + self->code = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); + REQUIRE(!map.TryLookup(123)); + REQUIRE(!map.TryRemove(123)); + + self->code = E_FAIL; + REQUIRE(!map.TryLookup(123)); + REQUIRE(!map.TryRemove(123)); + } \ No newline at end of file