Skip to content

Commit

Permalink
fix(web): fix type casting for JSString in error parsing (#12698)
Browse files Browse the repository at this point in the history
* fix(web): fix type casting for JSString

* refactor
  • Loading branch information
Lyokone committed Apr 24, 2024
1 parent 471b507 commit 93efcff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/_flutterfire_internals/lib/_flutterfire_internals.dart
Expand Up @@ -16,6 +16,8 @@ import 'package:firebase_core/firebase_core.dart';
import 'src/interop_shimmer.dart'
if (dart.library.js_interop) 'package:firebase_core_web/firebase_core_web_interop.dart'
as core_interop;
import 'src/interop_shimmer.dart' if (dart.library.js_interop) 'dart:js_interop'
as js_interop;

export 'src/exception.dart';

Expand Down Expand Up @@ -50,17 +52,26 @@ extension ObjectX<T> on T? {
}
}

// Necessary because of the conditional import
String _safeConvertFromPossibleJSObject(dynamic value) {
if (value is js_interop.JSAny) {
return (value as js_interop.JSString).toDart;
} else {
return value as String;
}
}

FirebaseException _firebaseExceptionFromCoreFirebaseError(
core_interop.JSError firebaseError, {
required String plugin,
required String Function(String) codeParser,
required String Function(String code, String message)? messageParser,
}) {
// ignore: unnecessary_cast
final convertCode = firebaseError.code as String;
final convertCode = _safeConvertFromPossibleJSObject(firebaseError.code);
final code = codeParser(convertCode);
// ignore: unnecessary_cast
final convertMessage = firebaseError.message as String;

final String convertMessage =
_safeConvertFromPossibleJSObject(firebaseError.message);
final message = messageParser != null
? messageParser(code, convertMessage)
: convertMessage.replaceFirst('(${firebaseError.code})', '');
Expand All @@ -81,8 +92,8 @@ FirebaseException _firebaseExceptionFromCoreFirebaseError(
/// See also https://github.com/dart-lang/sdk/issues/30741
bool _testException(Object? objectException) {
final exception = objectException! as core_interop.JSError;
// ignore: unnecessary_cast
final message = exception.message as String;

final message = _safeConvertFromPossibleJSObject(exception.message);
// Firestore web does not contain `Firebase` in the message so we check the exception itself.
return message.contains('Firebase') ||
exception.toString().contains('FirebaseError');
Expand Down
6 changes: 6 additions & 0 deletions packages/_flutterfire_internals/lib/src/interop_shimmer.dart
Expand Up @@ -12,3 +12,9 @@ abstract class JSError {
String get stack;
dynamic get serverResponse;
}

abstract class JSAny {}

abstract class JSString {
String get toDart;
}

0 comments on commit 93efcff

Please sign in to comment.