Skip to content

Commit

Permalink
Bug 1708643 - Add generic predicate and fallback functions for QM_OR_…
Browse files Browse the repository at this point in the history
…ELSE_(WARN|NOTE|LOG)_IF; r=dom-storage-reviewers,asuth

Differential Revision: https://phabricator.services.mozilla.com/D114935
  • Loading branch information
janvarga committed May 30, 2021
1 parent 08cbb76 commit 5977b6f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
28 changes: 28 additions & 0 deletions dom/quota/QuotaCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,34 @@ auto ErrToDefaultOkOrErr(nsresult aValue) -> Result<V, nsresult> {
return Err(aValue);
}

// Helper template function so that QM_TRY predicates checking for a specific
// error can be concisely written as IsSpecificError<NS_SOME_ERROR> instead of
// as a more verbose lambda.
template <nsresult ErrorValue>
bool IsSpecificError(const nsresult aValue) {
return aValue == ErrorValue;
}

// Helper template function so that QM_TRY fallback functions that are
// converting errors into specific in-band success values can be concisely
// written as ErrToOk<SuccessValueToReturn> (with the return type inferred).
// For example, many file-related APIs that access information about a file may
// return an nsresult error code if the file does not exist. From an
// application perspective, the file not existing is not actually exceptional
// and can instead be handled by the success case.
template <auto SuccessValue, typename V = decltype(SuccessValue)>
auto ErrToOk(const nsresult aValue) -> Result<V, nsresult> {
return V{SuccessValue};
}

// Helper template function so that QM_TRY fallback functions that are
// suppressing errors by converting them into (generic) success can be
// concisely written as ErrToDefaultOk<>.
template <typename V = mozilla::Ok>
auto ErrToDefaultOk(const nsresult aValue) -> Result<V, nsresult> {
return V{};
}

// TODO: Maybe move this to mfbt/ResultExtensions.h
template <typename R, typename Func, typename... Args>
Result<R, nsresult> ToResultGet(const Func& aFunc, Args&&... aArgs) {
Expand Down
47 changes: 47 additions & 0 deletions dom/quota/test/gtest/TestQuotaCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,53 @@ TEST(QuotaCommon_ErrToDefaultOkOrErr, NsCOMPtr_Err)
EXPECT_EQ(res.unwrapErr(), NS_ERROR_UNEXPECTED);
}

TEST(QuotaCommon_IsSpecificError, Match)
{ EXPECT_TRUE(IsSpecificError<NS_ERROR_FAILURE>(NS_ERROR_FAILURE)); }

TEST(QuotaCommon_IsSpecificError, Mismatch)
{ EXPECT_FALSE(IsSpecificError<NS_ERROR_FAILURE>(NS_ERROR_UNEXPECTED)); }

TEST(QuotaCommon_ErrToOk, Bool_True)
{
auto res = ErrToOk<true>(NS_ERROR_FAILURE);
EXPECT_TRUE(res.isOk());
EXPECT_EQ(res.unwrap(), true);
}

TEST(QuotaCommon_ErrToOk, Bool_False)
{
auto res = ErrToOk<false>(NS_ERROR_FAILURE);
EXPECT_TRUE(res.isOk());
EXPECT_EQ(res.unwrap(), false);
}

TEST(QuotaCommon_ErrToOk, Int_42)
{
auto res = ErrToOk<42>(NS_ERROR_FAILURE);
EXPECT_TRUE(res.isOk());
EXPECT_EQ(res.unwrap(), 42);
}

TEST(QuotaCommon_ErrToOk, NsCOMPtr_nullptr)
{
auto res = ErrToOk<nullptr, nsCOMPtr<nsISupports>>(NS_ERROR_FAILURE);
EXPECT_TRUE(res.isOk());
EXPECT_EQ(res.unwrap(), nullptr);
}

TEST(QuotaCommon_ErrToDefaultOk, Ok)
{
auto res = ErrToDefaultOk<Ok>(NS_ERROR_FAILURE);
EXPECT_TRUE(res.isOk());
}

TEST(QuotaCommon_ErrToDefaultOk, NsCOMPtr)
{
auto res = ErrToDefaultOk<nsCOMPtr<nsISupports>>(NS_ERROR_FAILURE);
EXPECT_TRUE(res.isOk());
EXPECT_EQ(res.unwrap(), nullptr);
}

class StringPairParameterized
: public ::testing::TestWithParam<std::pair<const char*, const char*>> {};

Expand Down

0 comments on commit 5977b6f

Please sign in to comment.