Skip to content

fix(auth,functions,web): rethrow non-Firebase errors instead of crashing in the error mappers - #18923

Merged
Lyokone merged 4 commits into
mainfrom
fix/web-auth-functions-error-mapping
Nov 10, 2026
Merged

Lyokone merged 4 commits into
mainfrom
fix/web-auth-functions-error-mapping

Conversation

@Lyokone

@Lyokone Lyokone commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Description

Functions (web): call(), stream() and the abort-signal helper cast every caught error to JSObject, so a Dart error became a TypeError. A JS rejection without a string code or message ({}, a plain Error, or the TypeError from a negative TimeLimit) hit a null check instead of propagating.

Auth (web): guardAuthExceptions, getFirebaseAuthException, verifyPhoneNumber and the reCAPTCHA verifier's render()/verify() cast caught errors to JSError, so a Dart error (for example one thrown by codeSent) became a TypeError. useAuthEmulator checked e is AuthError, which is true for any JS object, then read a non-null code, so a JS error without code crashed.

Fix: Dart errors are rethrown unchanged with their stack trace (the auth Future path uses catchError(test:), like guardWebExceptions). JS errors still go to the mappers, which read code/message null-safely. Firebase errors are mapped exactly as before.

Tests (--platform chrome, and --wasm):

  • cloud_functions_web/test/https_callable_web_test.dart: call() and stream() with a FunctionsError, a JS rejection without code or message, a plain JS Error, a Dart error, and an invalid abort signal. Six fail without the fix.
  • firebase_auth_web/test/guard_auth_exceptions_test.dart, phone_verification_errors_test.dart, use_auth_emulator_test.dart, and a Dart-error case in get_firebase_auth_exception_test.dart. The rethrows a Dart error… tests and maps a JS error without code to an unknown error fail without the fix.

Related Issues

Follow-up to the review of #18896, which fixes the same bug in the shared guardWebExceptions.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
This will ensure a smooth and quick review process. Updating the pubspec.yaml and changelogs is not required.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

…hing in the error mapper

The catch blocks in `call()`, `stream()` and the abort-signal helper cast
every caught error to `JSObject`, so a Dart error became a `TypeError`.
`convertFirebaseFunctionsException` also force-unwrapped `code` and
`message`, so a JS rejection without them (a plain `Error`, `{}`, or the
`TypeError` from `AbortSignal.timeout()` with a negative delay) became a
null-check error.

Only JS objects with a string `code` are now converted. Anything else is
rethrown with its original stack trace, and `code`/`message` are read
null-safely.
`guardAuthExceptions` cast every sync error to `JSError`, and its Future
path passed every error to `getFirebaseAuthException`, which did the same
cast. A Dart error thrown inside a guarded call therefore became a
`TypeError` and lost its stack trace.

`useAuthEmulator` checked `e is AuthError`, which is true for any JS
object because `@staticInterop` types are erased at runtime, then read
the non-nullable `code`, so a JS error without `code` crashed.

Dart errors are now rethrown unchanged (the Future path uses
`catchError(test:)`), `getFirebaseAuthException` returns an `unknown`
error for non-JS input, and `useAuthEmulator` compares `code` in JS.
Firebase Auth errors and other JS errors are mapped as before.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves error handling in cloud_functions_web and firebase_auth_web by ensuring that caught errors are verified as JS objects before processing, preventing runtime issues on dart2js and dart2wasm. It also adds comprehensive tests for these scenarios. The review feedback correctly identifies several violations of the repository's style guide prohibiting the null-assertion operator (!) in both implementation and test files, suggesting safer alternatives using direct casting or flow analysis.

Comment on lines +23 to +26
String? _stringProperty(JSObject object, String property) {
final value = object.getProperty(property.toJS);
return value.isA<JSString>() ? (value! as JSString).toDart : null;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

According to the repository style guide (Rule 23), the ! operator is prohibited. We should avoid using ! and instead use flow analysis or direct casting.

String? _stringProperty(JSObject object, String property) {
  final value = object.getProperty(property.toJS);
  if (value.isA<JSString>()) {
    return (value as JSString).toDart;
  }
  return null;
}
References
  1. Null Safety: NO ! operator. Use ? and flow analysis (e.g. if (x != null)). (link)

Comment on lines +24 to +30
JSObject _functionsError() {
final error = (globalContext['Error']! as JSFunction)
.callAsConstructor<JSObject>('not allowed'.toJS);
error['name'] = 'FirebaseError'.toJS;
error['code'] = 'functions/permission-denied'.toJS;
error['details'] = {'reason': 'quota'}.jsify();
return error;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

According to the repository style guide (Rule 23), the ! operator is prohibited. We can avoid it by casting directly using as JSFunction.

JSObject _functionsError() {
  final error = (globalContext['Error'] as JSFunction)
      .callAsConstructor<JSObject>('not allowed'.toJS);
  error['name'] = 'FirebaseError'.toJS;
  error['code'] = 'functions/permission-denied'.toJS;
  error['details'] = {'reason': 'quota'}.jsify();
  return error;
References
  1. Null Safety: NO ! operator. Use ? and flow analysis (e.g. if (x != null)). (link)

Comment on lines +33 to +34
JSPromise _rejectWith(JSAny error) => (globalContext['Promise']! as JSObject)
.callMethod<JSPromise>('reject'.toJS, error);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

According to the repository style guide (Rule 23), the ! operator is prohibited. We can avoid it by casting directly using as JSObject.

JSPromise _rejectWith(JSAny error) => (globalContext['Promise'] as JSObject)
    .callMethod<JSPromise>('reject'.toJS, error);
References
  1. Null Safety: NO ! operator. Use ? and flow analysis (e.g. if (x != null)). (link)

Comment on lines +16 to +20
JSObject _firebaseAuthError() {
final error = (globalContext['Error']! as JSFunction)
.callAsConstructor<JSObject>(
'Firebase: Error (auth/user-disabled).'.toJS,
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

According to the repository style guide (Rule 23), the ! operator is prohibited. We can avoid it by casting directly using as JSFunction.

JSObject _firebaseAuthError() {
  final error = (globalContext['Error'] as JSFunction)
      .callAsConstructor<JSObject>(
        'Firebase: Error (auth/user-disabled).'.toJS,
      );
References
  1. Null Safety: NO ! operator. Use ? and flow analysis (e.g. if (x != null)). (link)

Comment on lines +31 to +33
JSObject _jsError(String message, {String? code}) {
final error = (globalContext['Error']! as JSFunction)
.callAsConstructor<JSObject>(message.toJS);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

According to the repository style guide (Rule 23), the ! operator is prohibited. We can avoid it by casting directly using as JSFunction.

JSObject _jsError(String message, {String? code}) {
  final error = (globalContext['Error'] as JSFunction)
      .callAsConstructor<JSObject>(message.toJS);
References
  1. Null Safety: NO ! operator. Use ? and flow analysis (e.g. if (x != null)). (link)

@Lyokone
Lyokone marked this pull request as ready for review November 9, 2026 14:52

@SelaseKay SelaseKay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verifyPhoneNumber calls codeSent inside that try. If codeSent throws, verificationFailed now receives unknown and the Future completes. The same happens for a failed cast of multiFactorSession before the JS call. render does this for an awaited rejection that is a Dart error. verify only catches a synchronous throw.

Let's rethrow when the caught value is not a JS object, with Error.throwWithStackTrace, and leave Firebase JS errors on the mapper.

…APTCHA verifier

`verifyPhoneNumber` passed every caught error to `verificationFailed`, so a
Dart error thrown by `codeSent`, or the cast of a `multiFactorSession` from
another platform, was reported as an `unknown` failure and the Future
completed. `RecaptchaVerifierFactoryWeb.render()` and `verify()` turned a
Dart error into an `unknown` FirebaseAuthException the same way.

These catch blocks now rethrow anything that is not a JS object with
`Error.throwWithStackTrace`, and leave JS errors to the mapper.
@Lyokone
Lyokone merged commit 77ebf86 into main Nov 10, 2026
51 of 52 checks passed
@Lyokone
Lyokone deleted the fix/web-auth-functions-error-mapping branch November 10, 2026 13:04
SelaseKay added a commit that referenced this pull request Nov 10, 2026
Keep the JS error check from #18923, and still store the emulator origin, throwing in debug when this instance is on production.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants