Skip to content

fix(web): fix type casting for JSString in error parsing #12698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions packages/_flutterfire_internals/lib/_flutterfire_internals.dart
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ abstract class JSError {
String get stack;
dynamic get serverResponse;
}

abstract class JSAny {}

abstract class JSString {
String get toDart;
}
Loading