diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/method_channel_multi_factor.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/method_channel_multi_factor.dart index 61fd98e22fb4..abbc3d5537b5 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/method_channel_multi_factor.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/method_channel_multi_factor.dart @@ -5,6 +5,7 @@ import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart'; import 'package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart'; import 'package:firebase_auth_platform_interface/src/method_channel/method_channel_user_credential.dart'; +import 'package:firebase_auth_platform_interface/src/method_channel/utils/exception.dart'; import 'package:firebase_auth_platform_interface/src/method_channel/utils/pigeon_helper.dart'; import 'package:firebase_auth_platform_interface/src/pigeon/messages.pigeon.dart'; @@ -16,8 +17,12 @@ class MethodChannelMultiFactor extends MultiFactorPlatform { @override Future getSession() async { - final pigeonObject = await _api.getSession(auth.app.name); - return MultiFactorSession(pigeonObject.id); + try { + final pigeonObject = await _api.getSession(auth.app.name); + return MultiFactorSession(pigeonObject.id); + } catch (e, stack) { + convertPlatformException(e, stack, fromPigeon: true); + } } @override @@ -39,14 +44,18 @@ class MethodChannelMultiFactor extends MultiFactorPlatform { throw ArgumentError('verificationId must not be null'); } - await _api.enrollPhone( - auth.app.name, - PigeonPhoneMultiFactorAssertion( - verificationId: verificationId, - verificationCode: verificationCode, - ), - displayName, - ); + try { + await _api.enrollPhone( + auth.app.name, + PigeonPhoneMultiFactorAssertion( + verificationId: verificationId, + verificationCode: verificationCode, + ), + displayName, + ); + } catch (e, stack) { + convertPlatformException(e, stack, fromPigeon: true); + } } else { throw UnimplementedError( 'Credential type ${_assertion.credential} is not supported yet', @@ -66,16 +75,24 @@ class MethodChannelMultiFactor extends MultiFactorPlatform { ); } - return _api.unenroll( - auth.app.name, - uidToUnenroll, - ); + try { + return _api.unenroll( + auth.app.name, + uidToUnenroll, + ); + } catch (e, stack) { + convertPlatformException(e, stack, fromPigeon: true); + } } @override Future> getEnrolledFactors() async { - final data = await _api.getEnrolledFactors(auth.app.name); - return multiFactorInfoPigeonToObject(data); + try { + final data = await _api.getEnrolledFactors(auth.app.name); + return multiFactorInfoPigeonToObject(data); + } catch (e, stack) { + convertPlatformException(e, stack, fromPigeon: true); + } } } @@ -112,18 +129,22 @@ class MethodChannelMultiFactorResolver extends MultiFactorResolverPlatform { throw ArgumentError('verificationId must not be null'); } - final data = await _api.resolveSignIn( - _resolverId, - PigeonPhoneMultiFactorAssertion( - verificationId: verificationId, - verificationCode: verificationCode, - ), - ); - - MethodChannelUserCredential userCredential = - MethodChannelUserCredential(_auth, data.cast()); - - return userCredential; + try { + final data = await _api.resolveSignIn( + _resolverId, + PigeonPhoneMultiFactorAssertion( + verificationId: verificationId, + verificationCode: verificationCode, + ), + ); + + MethodChannelUserCredential userCredential = + MethodChannelUserCredential(_auth, data.cast()); + + return userCredential; + } catch (e, stack) { + convertPlatformException(e, stack, fromPigeon: true); + } } else { throw UnimplementedError( 'Credential type ${_assertion.credential} is not supported yet', diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart index 211323cc4809..652857fe940c 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart @@ -14,13 +14,17 @@ import 'package:flutter/services.dart'; /// Catches a [PlatformException] and converts it into a [FirebaseAuthException] /// if it was intentionally caught on the native platform. -Never convertPlatformException(Object exception, StackTrace stackTrace) { +Never convertPlatformException( + Object exception, + StackTrace stackTrace, { + bool fromPigeon = false, +}) { if (exception is! PlatformException) { Error.throwWithStackTrace(exception, stackTrace); } Error.throwWithStackTrace( - platformExceptionToFirebaseAuthException(exception), + platformExceptionToFirebaseAuthException(exception, fromPigeon: fromPigeon), stackTrace, ); } @@ -32,8 +36,17 @@ Never convertPlatformException(Object exception, StackTrace stackTrace) { /// messages which can be converted into user friendly exceptions. // TODO(rousselGit): Should this return a FirebaseAuthException to avoid having to cast? FirebaseException platformExceptionToFirebaseAuthException( - PlatformException platformException, -) { + PlatformException platformException, { + bool fromPigeon = false, +}) { + if (fromPigeon) { + return FirebaseAuthException( + code: platformException.code, + // Remove leading classname from message + message: platformException.message?.split(': ').last, + ); + } + Map? details = platformException.details != null ? Map.from(platformException.details) : null; diff --git a/packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_multi_factor.dart b/packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_multi_factor.dart index 9c45e578cb84..794e31a35e30 100644 --- a/packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_multi_factor.dart +++ b/packages/firebase_auth/firebase_auth_web/lib/src/firebase_auth_web_multi_factor.dart @@ -58,9 +58,13 @@ class MultiFactorWeb extends MultiFactorPlatform { ); } - return _webMultiFactorUser.unenroll( - uidToUnenroll, - ); + try { + return _webMultiFactorUser.unenroll( + uidToUnenroll, + ); + } catch (e) { + throw getFirebaseAuthException(e); + } } @override @@ -106,11 +110,15 @@ class MultiFactorResolverWeb extends MultiFactorResolverPlatform { ) async { final webAssertion = assertion as MultiFactorAssertionWeb; - return UserCredentialWeb( - _auth, - await _webMultiFactorResolver.resolveSignIn(webAssertion.assertion), - _webAuth, - ); + try { + return UserCredentialWeb( + _auth, + await _webMultiFactorResolver.resolveSignIn(webAssertion.assertion), + _webAuth, + ); + } catch (e) { + throw getFirebaseAuthException(e); + } } }