Skip to content

Commit

Permalink
fix(firestore): transaction.get() should throw FirebaseException
Browse files Browse the repository at this point in the history
…on exception. (#12064)
  • Loading branch information
russellwheatley committed Dec 27, 2023
1 parent 1cf38b8 commit 3cfc501
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void runTransactionTests() {
expect(e.code, equals('permission-denied'));
return;
} catch (e) {
fail('Transaction threw invalid exeption');
fail('Transaction threw invalid exception');
}
});

Expand All @@ -249,6 +249,25 @@ void runTransactionTests() {
);
});

testWidgets(
'should throw a native error, and convert to a [FirebaseException]',
(_) async {
DocumentReference<Map<String, dynamic>> documentReference =
firestore.doc('not-allowed/document');

try {
await firestore.runTransaction((Transaction transaction) async {
await transaction.get(documentReference);
});
fail('Transaction should not have resolved');
} on FirebaseException catch (e) {
expect(e.code, equals('permission-denied'));
return;
} catch (e) {
fail('Transaction threw invalid exception');
}
});

// ignore: todo
// TODO(Salakar): Test seems to fail sometimes. Will look at in a future PR.
// testWidgets('support returning any value, e.g. a [DocumentSnapshot]', (_) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import 'dart:async';

import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';
import 'package:cloud_firestore_platform_interface/src/method_channel/utils/exception.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/services.dart';

Expand Down Expand Up @@ -45,16 +46,19 @@ class MethodChannelTransaction extends TransactionPlatform {
Future<DocumentSnapshotPlatform> get(String documentPath) async {
assert(_commands.isEmpty,
'Transactions require all reads to be executed before all writes.');

final result = await MethodChannelFirebaseFirestore.pigeonChannel
.transactionGet(pigeonApp, _transactionId, documentPath);

return DocumentSnapshotPlatform(
_firestore,
documentPath,
result.data,
result.metadata,
);
try {
final result = await MethodChannelFirebaseFirestore.pigeonChannel
.transactionGet(pigeonApp, _transactionId, documentPath);

return DocumentSnapshotPlatform(
_firestore,
documentPath,
result.data,
result.metadata,
);
} catch (e, stack) {
convertPlatformException(e, stack);
}
}

@override
Expand Down

0 comments on commit 3cfc501

Please sign in to comment.