File tree

3 files changed

+82
-40
lines changed

3 files changed

+82
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
66
import 'package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart';
77
import 'package:firebase_auth_platform_interface/src/method_channel/method_channel_user_credential.dart';
8+
import 'package:firebase_auth_platform_interface/src/method_channel/utils/exception.dart';
89
import 'package:firebase_auth_platform_interface/src/method_channel/utils/pigeon_helper.dart';
910
import 'package:firebase_auth_platform_interface/src/pigeon/messages.pigeon.dart';
1011

@@ -16,8 +17,12 @@ class MethodChannelMultiFactor extends MultiFactorPlatform {
1617

1718
@override
1819
Future<MultiFactorSession> getSession() async {
19-
final pigeonObject = await _api.getSession(auth.app.name);
20-
return MultiFactorSession(pigeonObject.id);
20+
try {
21+
final pigeonObject = await _api.getSession(auth.app.name);
22+
return MultiFactorSession(pigeonObject.id);
23+
} catch (e, stack) {
24+
convertPlatformException(e, stack, fromPigeon: true);
25+
}
2126
}
2227

2328
@override
@@ -39,14 +44,18 @@ class MethodChannelMultiFactor extends MultiFactorPlatform {
3944
throw ArgumentError('verificationId must not be null');
4045
}
4146

42-
await _api.enrollPhone(
43-
auth.app.name,
44-
PigeonPhoneMultiFactorAssertion(
45-
verificationId: verificationId,
46-
verificationCode: verificationCode,
47-
),
48-
displayName,
49-
);
47+
try {
48+
await _api.enrollPhone(
49+
auth.app.name,
50+
PigeonPhoneMultiFactorAssertion(
51+
verificationId: verificationId,
52+
verificationCode: verificationCode,
53+
),
54+
displayName,
55+
);
56+
} catch (e, stack) {
57+
convertPlatformException(e, stack, fromPigeon: true);
58+
}
5059
} else {
5160
throw UnimplementedError(
5261
'Credential type ${_assertion.credential} is not supported yet',
@@ -66,16 +75,24 @@ class MethodChannelMultiFactor extends MultiFactorPlatform {
6675
);
6776
}
6877

69-
return _api.unenroll(
70-
auth.app.name,
71-
uidToUnenroll,
72-
);
78+
try {
79+
return _api.unenroll(
80+
auth.app.name,
81+
uidToUnenroll,
82+
);
83+
} catch (e, stack) {
84+
convertPlatformException(e, stack, fromPigeon: true);
85+
}
7386
}
7487

7588
@override
7689
Future<List<MultiFactorInfo>> getEnrolledFactors() async {
77-
final data = await _api.getEnrolledFactors(auth.app.name);
78-
return multiFactorInfoPigeonToObject(data);
90+
try {
91+
final data = await _api.getEnrolledFactors(auth.app.name);
92+
return multiFactorInfoPigeonToObject(data);
93+
} catch (e, stack) {
94+
convertPlatformException(e, stack, fromPigeon: true);
95+
}
7996
}
8097
}
8198

@@ -112,18 +129,22 @@ class MethodChannelMultiFactorResolver extends MultiFactorResolverPlatform {
112129
throw ArgumentError('verificationId must not be null');
113130
}
114131

115-
final data = await _api.resolveSignIn(
116-
_resolverId,
117-
PigeonPhoneMultiFactorAssertion(
118-
verificationId: verificationId,
119-
verificationCode: verificationCode,
120-
),
121-
);
122-
123-
MethodChannelUserCredential userCredential =
124-
MethodChannelUserCredential(_auth, data.cast<String, dynamic>());
125-
126-
return userCredential;
132+
try {
133+
final data = await _api.resolveSignIn(
134+
_resolverId,
135+
PigeonPhoneMultiFactorAssertion(
136+
verificationId: verificationId,
137+
verificationCode: verificationCode,
138+
),
139+
);
140+
141+
MethodChannelUserCredential userCredential =
142+
MethodChannelUserCredential(_auth, data.cast<String, dynamic>());
143+
144+
return userCredential;
145+
} catch (e, stack) {
146+
convertPlatformException(e, stack, fromPigeon: true);
147+
}
127148
} else {
128149
throw UnimplementedError(
129150
'Credential type ${_assertion.credential} is not supported yet',
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ import 'package:flutter/services.dart';
1414

1515
/// Catches a [PlatformException] and converts it into a [FirebaseAuthException]
1616
/// if it was intentionally caught on the native platform.
17-
Never convertPlatformException(Object exception, StackTrace stackTrace) {
17+
Never convertPlatformException(
18+
Object exception,
19+
StackTrace stackTrace, {
20+
bool fromPigeon = false,
21+
}) {
1822
if (exception is! PlatformException) {
1923
Error.throwWithStackTrace(exception, stackTrace);
2024
}
2125

2226
Error.throwWithStackTrace(
23-
platformExceptionToFirebaseAuthException(exception),
27+
platformExceptionToFirebaseAuthException(exception, fromPigeon: fromPigeon),
2428
stackTrace,
2529
);
2630
}
@@ -32,8 +36,17 @@ Never convertPlatformException(Object exception, StackTrace stackTrace) {
3236
/// messages which can be converted into user friendly exceptions.
3337
// TODO(rousselGit): Should this return a FirebaseAuthException to avoid having to cast?
3438
FirebaseException platformExceptionToFirebaseAuthException(
35-
PlatformException platformException,
36-
) {
39+
PlatformException platformException, {
40+
bool fromPigeon = false,
41+
}) {
42+
if (fromPigeon) {
43+
return FirebaseAuthException(
44+
code: platformException.code,
45+
// Remove leading classname from message
46+
message: platformException.message?.split(': ').last,
47+
);
48+
}
49+
3750
Map<String, dynamic>? details = platformException.details != null
3851
? Map<String, dynamic>.from(platformException.details)
3952
: null;
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ class MultiFactorWeb extends MultiFactorPlatform {
5858
);
5959
}
6060

61-
return _webMultiFactorUser.unenroll(
62-
uidToUnenroll,
63-
);
61+
try {
62+
return _webMultiFactorUser.unenroll(
63+
uidToUnenroll,
64+
);
65+
} catch (e) {
66+
throw getFirebaseAuthException(e);
67+
}
6468
}
6569

6670
@override
@@ -106,11 +110,15 @@ class MultiFactorResolverWeb extends MultiFactorResolverPlatform {
106110
) async {
107111
final webAssertion = assertion as MultiFactorAssertionWeb;
108112

109-
return UserCredentialWeb(
110-
_auth,
111-
await _webMultiFactorResolver.resolveSignIn(webAssertion.assertion),
112-
_webAuth,
113-
);
113+
try {
114+
return UserCredentialWeb(
115+
_auth,
116+
await _webMultiFactorResolver.resolveSignIn(webAssertion.assertion),
117+
_webAuth,
118+
);
119+
} catch (e) {
120+
throw getFirebaseAuthException(e);
121+
}
114122
}
115123
}
116124

0 commit comments

Comments
 (0)