Skip to content

Commit 9ce6a12

Browse files
authored
Firestore: Add more details to the assertion failure in OrderBy::Compare() (#9305)
1 parent 22032b6 commit 9ce6a12

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Firestore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
- [changed] Add more details to the assertion failure in OrderBy::Compare() to
3+
help with future debugging (#9258).
4+
15
# v8.12.0
26
- [fixed] Fixed an AppCheck issue that caused Firestore listeners to stop
37
working and receive a "Permission Denied" error. This issue only occurred for

Firestore/core/src/core/order_by.cc

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Firestore/core/src/core/order_by.h"
1818

1919
#include <ostream>
20+
#include <sstream>
2021

2122
#include "Firestore/core/src/model/document.h"
2223
#include "Firestore/core/src/model/value_util.h"
@@ -31,6 +32,38 @@ using model::Document;
3132
using model::FieldPath;
3233
using util::ComparisonResult;
3334

35+
namespace {
36+
37+
void AssertBothOptionalsHaveValues(
38+
const model::FieldPath& field_path,
39+
const absl::optional<google_firestore_v1_Value>& value1,
40+
const absl::optional<google_firestore_v1_Value>& value2,
41+
const Document& lhs,
42+
const Document& rhs) {
43+
if (value1.has_value() && value2.has_value()) {
44+
return;
45+
}
46+
47+
std::ostringstream ss;
48+
ss << "Trying to compare documents on fields that don't exist;"
49+
<< " field_path=" << field_path.CanonicalString()
50+
<< ", lhs=" << lhs->key().ToString() << ", rhs=" << rhs->key().ToString()
51+
<< ", value1.has_value()=" << (value1.has_value() ? "true" : "false")
52+
<< ", value2.has_value()=" << (value2.has_value() ? "true" : "false");
53+
54+
if (value1.has_value()) {
55+
ss << ", value1=" << value1->ToString();
56+
}
57+
if (value2.has_value()) {
58+
ss << ", value2=" << value2->ToString();
59+
}
60+
61+
std::string message = ss.str();
62+
HARD_FAIL(message.c_str());
63+
}
64+
65+
} // namespace
66+
3467
ComparisonResult OrderBy::Compare(const Document& lhs,
3568
const Document& rhs) const {
3669
ComparisonResult result;
@@ -39,8 +72,7 @@ ComparisonResult OrderBy::Compare(const Document& lhs,
3972
} else {
4073
absl::optional<google_firestore_v1_Value> value1 = lhs->field(field_);
4174
absl::optional<google_firestore_v1_Value> value2 = rhs->field(field_);
42-
HARD_ASSERT(value1.has_value() && value2.has_value(),
43-
"Trying to compare documents on fields that don't exist.");
75+
AssertBothOptionalsHaveValues(field_, value1, value2, lhs, rhs);
4476
result = model::Compare(*value1, *value2);
4577
}
4678

0 commit comments

Comments
 (0)