Skip to content

Commit 33d90f4

Browse files
Googlera-maurice
authored andcommitted
Plumb through error messages for Query and Document listens in C++ and Unity.
NOTE: This changelist is a breaking change in that it changes the public C++ Firestore API in a backwards-incompatible manner. PiperOrigin-RevId: 330757239
1 parent 65e5703 commit 33d90f4

29 files changed

+218
-206
lines changed

firestore/src/android/document_reference_android.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ Future<void> DocumentReferenceInternal::Delete() {
151151

152152
ListenerRegistration DocumentReferenceInternal::AddSnapshotListener(
153153
MetadataChanges metadata_changes,
154-
std::function<void(const DocumentSnapshot&, Error)> callback) {
154+
std::function<void(const DocumentSnapshot&, Error, const std::string&)>
155+
callback) {
155156
LambdaEventListener<DocumentSnapshot>* listener =
156157
new LambdaEventListener<DocumentSnapshot>(firebase::Move(callback));
157158
return AddSnapshotListener(metadata_changes, listener,

firestore/src/android/document_reference_android.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ class DocumentReferenceInternal : public Wrapper {
146146
*/
147147
ListenerRegistration AddSnapshotListener(
148148
MetadataChanges metadata_changes,
149-
std::function<void(const DocumentSnapshot&, Error)> callback);
149+
std::function<void(const DocumentSnapshot&, Error, const std::string&)>
150+
callback);
150151
#endif // defined(FIREBASE_USE_STD_FUNCTION)
151152

152153
/**

firestore/src/android/event_listener_android.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "firestore/src/android/firebase_firestore_exception_android.h"
1111
#include "firestore/src/android/query_snapshot_android.h"
1212
#include "firestore/src/android/util_android.h"
13+
#include "firestore/src/common/util.h"
1314
#include "firestore/src/include/firebase/firestore/query_snapshot.h"
1415
#include "firestore/src/jni/env.h"
1516
#include "firebase/firestore/firestore_errors.h"
@@ -65,15 +66,17 @@ void EventListenerInternal::DocumentEventListenerNativeOnEvent(
6566
reinterpret_cast<EventListener<DocumentSnapshot>*>(listener_ptr);
6667
Error error_code =
6768
FirebaseFirestoreExceptionInternal::ToErrorCode(env, error);
69+
std::string error_message =
70+
FirebaseFirestoreExceptionInternal::ToString(env, error);
6871
if (error_code != Error::kErrorOk) {
69-
listener->OnEvent(DocumentSnapshot{}, error_code);
72+
listener->OnEvent(DocumentSnapshot{}, error_code, error_message);
7073
return;
7174
}
7275

7376
FirestoreInternal* firestore =
7477
reinterpret_cast<FirestoreInternal*>(firestore_ptr);
7578
DocumentSnapshot snapshot(new DocumentSnapshotInternal{firestore, value});
76-
listener->OnEvent(snapshot, error_code);
79+
listener->OnEvent(snapshot, error_code, error_message);
7780
}
7881

7982
/* static */
@@ -87,15 +90,17 @@ void EventListenerInternal::QueryEventListenerNativeOnEvent(
8790
reinterpret_cast<EventListener<QuerySnapshot>*>(listener_ptr);
8891
Error error_code =
8992
FirebaseFirestoreExceptionInternal::ToErrorCode(env, error);
93+
std::string error_message =
94+
FirebaseFirestoreExceptionInternal::ToString(env, error);
9095
if (error_code != Error::kErrorOk) {
91-
listener->OnEvent(QuerySnapshot{}, error_code);
96+
listener->OnEvent(QuerySnapshot{}, error_code, error_message);
9297
return;
9398
}
9499

95100
FirestoreInternal* firestore =
96101
reinterpret_cast<FirestoreInternal*>(firestore_ptr);
97102
QuerySnapshot snapshot(new QuerySnapshotInternal{firestore, value});
98-
listener->OnEvent(snapshot, error_code);
103+
listener->OnEvent(snapshot, error_code, error_message);
99104
}
100105

101106
/* static */
@@ -108,7 +113,7 @@ void EventListenerInternal::VoidEventListenerNativeOnEvent(JNIEnv* env,
108113
EventListener<void>* listener =
109114
reinterpret_cast<EventListener<void>*>(listener_ptr);
110115

111-
listener->OnEvent(Error::kErrorOk);
116+
listener->OnEvent(Error::kErrorOk, EmptyString());
112117
}
113118

114119
Local<Object> EventListenerInternal::Create(

firestore/src/android/lambda_event_listener.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ namespace firestore {
1818
template <typename T>
1919
class LambdaEventListener : public EventListener<T> {
2020
public:
21-
LambdaEventListener(std::function<void(const T&, Error)> callback)
21+
LambdaEventListener(
22+
std::function<void(const T&, Error, const std::string&)> callback)
2223
: callback_(firebase::Move(callback)) {
2324
FIREBASE_ASSERT(callback_);
2425
}
2526

26-
void OnEvent(const T& value, Error error) override {
27-
callback_(value, error);
27+
void OnEvent(const T& value, Error error_code,
28+
const std::string& error_message) override {
29+
callback_(value, error_code, error_message);
2830
}
2931

3032
private:
31-
std::function<void(const T&, Error)> callback_;
33+
std::function<void(const T&, Error, const std::string&)> callback_;
3234
};
3335

3436
template <>
@@ -39,7 +41,7 @@ class LambdaEventListener<void> : public EventListener<void> {
3941
FIREBASE_ASSERT(callback_);
4042
}
4143

42-
void OnEvent(Error) override { callback_(); }
44+
void OnEvent(Error, const std::string&) override { callback_(); }
4345

4446
private:
4547
std::function<void()> callback_;

firestore/src/android/query_android.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ Query QueryInternal::WithBound(query::Method method,
196196
#if defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
197197
ListenerRegistration QueryInternal::AddSnapshotListener(
198198
MetadataChanges metadata_changes,
199-
std::function<void(const QuerySnapshot&, Error)> callback) {
199+
std::function<void(const QuerySnapshot&, Error, const std::string&)>
200+
callback) {
200201
LambdaEventListener<QuerySnapshot>* listener =
201202
new LambdaEventListener<QuerySnapshot>(firebase::Move(callback));
202203
return AddSnapshotListener(metadata_changes, listener,

firestore/src/android/query_android.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ class QueryInternal : public Wrapper {
406406
*/
407407
ListenerRegistration AddSnapshotListener(
408408
MetadataChanges metadata_changes,
409-
std::function<void(const QuerySnapshot&, Error)> callback);
409+
std::function<void(const QuerySnapshot&, Error, const std::string&)>
410+
callback);
410411

411412
#endif // defined(FIREBASE_USE_STD_FUNCTION)
412413

firestore/src/common/document_reference.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,16 @@ Future<void> DocumentReference::Delete() {
152152

153153
#if defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
154154
ListenerRegistration DocumentReference::AddSnapshotListener(
155-
std::function<void(const DocumentSnapshot&, Error)> callback) {
155+
std::function<void(const DocumentSnapshot&, Error, const std::string&)>
156+
callback) {
156157
return AddSnapshotListener(MetadataChanges::kExclude,
157158
firebase::Move(callback));
158159
}
159160

160161
ListenerRegistration DocumentReference::AddSnapshotListener(
161162
MetadataChanges metadata_changes,
162-
std::function<void(const DocumentSnapshot&, Error)> callback) {
163+
std::function<void(const DocumentSnapshot&, Error, const std::string&)>
164+
callback) {
163165
FIREBASE_ASSERT_MESSAGE(callback, "invalid callback parameter is passed in.");
164166
if (!internal_) return {};
165167
return internal_->AddSnapshotListener(metadata_changes,

firestore/src/common/query.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,16 @@ Future<QuerySnapshot> Query::Get(Source source) const {
244244

245245
#if defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
246246
ListenerRegistration Query::AddSnapshotListener(
247-
std::function<void(const QuerySnapshot&, Error)> callback) {
247+
std::function<void(const QuerySnapshot&, Error, const std::string&)>
248+
callback) {
248249
return AddSnapshotListener(MetadataChanges::kExclude,
249250
firebase::Move(callback));
250251
}
251252

252253
ListenerRegistration Query::AddSnapshotListener(
253254
MetadataChanges metadata_changes,
254-
std::function<void(const QuerySnapshot&, Error)> callback) {
255+
std::function<void(const QuerySnapshot&, Error, const std::string&)>
256+
callback) {
255257
FIREBASE_ASSERT_MESSAGE(callback, "invalid callback parameter is passed in.");
256258
if (!internal_) return {};
257259
return internal_->AddSnapshotListener(metadata_changes,

firestore/src/include/firebase/csharp/document_event_listener.cc

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
#include "firebase/csharp/document_event_listener.h"
22

3-
#include "app/src/assert.h"
43
#include "firebase/firestore/document_reference.h"
54

65
namespace firebase {
76
namespace firestore {
87
namespace csharp {
98

10-
void DocumentEventListener::OnEvent(const DocumentSnapshot& value,
11-
Error error) {
12-
// Ownership of this pointer is passed into the C# handler
13-
auto* copy = new DocumentSnapshot(value);
14-
15-
callback_(callback_id_, copy, error);
16-
}
17-
18-
/* static */
19-
ListenerRegistration DocumentEventListener::AddListenerTo(
9+
ListenerRegistration AddDocumentSnapshotListener(
2010
DocumentReference* reference, MetadataChanges metadata_changes,
2111
int32_t callback_id, DocumentEventListenerCallback callback) {
22-
DocumentEventListener listener(callback_id, callback);
23-
2412
return reference->AddSnapshotListener(
2513
metadata_changes,
26-
[listener](const DocumentSnapshot& value, Error error) mutable {
27-
listener.OnEvent(value, error);
14+
[callback, callback_id](const DocumentSnapshot& value, Error error_code,
15+
const std::string& error_message) {
16+
// Ownership of the DocumentSnapshot pointer is passed to C#.
17+
callback(callback_id, new DocumentSnapshot(value), error_code,
18+
error_message.c_str());
2819
});
2920
}
3021

firestore/src/include/firebase/csharp/document_event_listener.h

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_DOCUMENT_EVENT_LISTENER_H_
22
#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_INCLUDE_FIREBASE_CSHARP_DOCUMENT_EVENT_LISTENER_H_
33

4-
#include <cstdint>
5-
6-
#include "app/src/callback.h"
7-
#include "app/src/mutex.h"
84
#include "firebase/firestore/document_snapshot.h"
9-
#include "firebase/firestore/event_listener.h"
105
#include "firebase/firestore/listener_registration.h"
116
#include "firebase/firestore/metadata_changes.h"
7+
#include "firebase/firestore/firestore_errors.h"
128

139
namespace firebase {
1410
namespace firestore {
@@ -25,33 +21,19 @@ namespace csharp {
2521
#endif
2622

2723
// The callbacks that are used by the listener, that need to reach back to C#
28-
// callbacks.
29-
typedef void(SWIGSTDCALL* DocumentEventListenerCallback)(int callback_id,
30-
void* snapshot,
31-
Error error);
32-
33-
// Provide a C++ implementation of the EventListener for DocumentSnapshot that
34-
// can forward the calls back to the C# delegates.
35-
class DocumentEventListener : public EventListener<DocumentSnapshot> {
36-
public:
37-
explicit DocumentEventListener(int32_t callback_id,
38-
DocumentEventListenerCallback callback)
39-
: callback_id_(callback_id), callback_(callback) {}
40-
41-
void OnEvent(const DocumentSnapshot& value, Error error) override;
42-
43-
// This method is a proxy to DocumentReference::AddSnapshotListener()
44-
// that can be easily called from C#. It allows our C# wrapper to
45-
// track user callbacks in a dictionary keyed off of a unique int
46-
// for each user callback and then raise the correct one later.
47-
static ListenerRegistration AddListenerTo(
48-
DocumentReference* reference, MetadataChanges metadata_changes,
49-
int32_t callback_id, DocumentEventListenerCallback callback);
50-
51-
private:
52-
int32_t callback_id_;
53-
DocumentEventListenerCallback callback_;
54-
};
24+
// callbacks. The error_message pointer is only valid for the duration of the
25+
// callback.
26+
typedef void(SWIGSTDCALL* DocumentEventListenerCallback)(
27+
int callback_id, DocumentSnapshot* snapshot, Error error_code,
28+
const char* error_message);
29+
30+
// This method is a proxy to DocumentReference::AddSnapshotListener()
31+
// that can be easily called from C#. It allows our C# wrapper to
32+
// track user callbacks in a dictionary keyed off of a unique int
33+
// for each user callback and then raise the correct one later.
34+
ListenerRegistration AddDocumentSnapshotListener(
35+
DocumentReference* reference, MetadataChanges metadata_changes,
36+
int32_t callback_id, DocumentEventListenerCallback callback);
5537

5638
} // namespace csharp
5739
} // namespace firestore

0 commit comments

Comments
 (0)