Skip to content

Commit 5977b6f

Browse files
committed
Bug 1708643 - Add generic predicate and fallback functions for QM_OR_ELSE_(WARN|NOTE|LOG)_IF; r=dom-storage-reviewers,asuth
Differential Revision: https://phabricator.services.mozilla.com/D114935
1 parent 08cbb76 commit 5977b6f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

dom/quota/QuotaCommon.h

+28
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,34 @@ auto ErrToDefaultOkOrErr(nsresult aValue) -> Result<V, nsresult> {
10221022
return Err(aValue);
10231023
}
10241024

1025+
// Helper template function so that QM_TRY predicates checking for a specific
1026+
// error can be concisely written as IsSpecificError<NS_SOME_ERROR> instead of
1027+
// as a more verbose lambda.
1028+
template <nsresult ErrorValue>
1029+
bool IsSpecificError(const nsresult aValue) {
1030+
return aValue == ErrorValue;
1031+
}
1032+
1033+
// Helper template function so that QM_TRY fallback functions that are
1034+
// converting errors into specific in-band success values can be concisely
1035+
// written as ErrToOk<SuccessValueToReturn> (with the return type inferred).
1036+
// For example, many file-related APIs that access information about a file may
1037+
// return an nsresult error code if the file does not exist. From an
1038+
// application perspective, the file not existing is not actually exceptional
1039+
// and can instead be handled by the success case.
1040+
template <auto SuccessValue, typename V = decltype(SuccessValue)>
1041+
auto ErrToOk(const nsresult aValue) -> Result<V, nsresult> {
1042+
return V{SuccessValue};
1043+
}
1044+
1045+
// Helper template function so that QM_TRY fallback functions that are
1046+
// suppressing errors by converting them into (generic) success can be
1047+
// concisely written as ErrToDefaultOk<>.
1048+
template <typename V = mozilla::Ok>
1049+
auto ErrToDefaultOk(const nsresult aValue) -> Result<V, nsresult> {
1050+
return V{};
1051+
}
1052+
10251053
// TODO: Maybe move this to mfbt/ResultExtensions.h
10261054
template <typename R, typename Func, typename... Args>
10271055
Result<R, nsresult> ToResultGet(const Func& aFunc, Args&&... aArgs) {

dom/quota/test/gtest/TestQuotaCommon.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,53 @@ TEST(QuotaCommon_ErrToDefaultOkOrErr, NsCOMPtr_Err)
14781478
EXPECT_EQ(res.unwrapErr(), NS_ERROR_UNEXPECTED);
14791479
}
14801480

1481+
TEST(QuotaCommon_IsSpecificError, Match)
1482+
{ EXPECT_TRUE(IsSpecificError<NS_ERROR_FAILURE>(NS_ERROR_FAILURE)); }
1483+
1484+
TEST(QuotaCommon_IsSpecificError, Mismatch)
1485+
{ EXPECT_FALSE(IsSpecificError<NS_ERROR_FAILURE>(NS_ERROR_UNEXPECTED)); }
1486+
1487+
TEST(QuotaCommon_ErrToOk, Bool_True)
1488+
{
1489+
auto res = ErrToOk<true>(NS_ERROR_FAILURE);
1490+
EXPECT_TRUE(res.isOk());
1491+
EXPECT_EQ(res.unwrap(), true);
1492+
}
1493+
1494+
TEST(QuotaCommon_ErrToOk, Bool_False)
1495+
{
1496+
auto res = ErrToOk<false>(NS_ERROR_FAILURE);
1497+
EXPECT_TRUE(res.isOk());
1498+
EXPECT_EQ(res.unwrap(), false);
1499+
}
1500+
1501+
TEST(QuotaCommon_ErrToOk, Int_42)
1502+
{
1503+
auto res = ErrToOk<42>(NS_ERROR_FAILURE);
1504+
EXPECT_TRUE(res.isOk());
1505+
EXPECT_EQ(res.unwrap(), 42);
1506+
}
1507+
1508+
TEST(QuotaCommon_ErrToOk, NsCOMPtr_nullptr)
1509+
{
1510+
auto res = ErrToOk<nullptr, nsCOMPtr<nsISupports>>(NS_ERROR_FAILURE);
1511+
EXPECT_TRUE(res.isOk());
1512+
EXPECT_EQ(res.unwrap(), nullptr);
1513+
}
1514+
1515+
TEST(QuotaCommon_ErrToDefaultOk, Ok)
1516+
{
1517+
auto res = ErrToDefaultOk<Ok>(NS_ERROR_FAILURE);
1518+
EXPECT_TRUE(res.isOk());
1519+
}
1520+
1521+
TEST(QuotaCommon_ErrToDefaultOk, NsCOMPtr)
1522+
{
1523+
auto res = ErrToDefaultOk<nsCOMPtr<nsISupports>>(NS_ERROR_FAILURE);
1524+
EXPECT_TRUE(res.isOk());
1525+
EXPECT_EQ(res.unwrap(), nullptr);
1526+
}
1527+
14811528
class StringPairParameterized
14821529
: public ::testing::TestWithParam<std::pair<const char*, const char*>> {};
14831530

0 commit comments

Comments
 (0)