Repository navigation
fix(auth,functions,web): rethrow non-Firebase errors instead of crashing in the error mappers - #18923
Conversation
…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.
There was a problem hiding this comment.
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.
| String? _stringProperty(JSObject object, String property) { | ||
| final value = object.getProperty(property.toJS); | ||
| return value.isA<JSString>() ? (value! as JSString).toDart : null; | ||
| } |
There was a problem hiding this comment.
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
- Null Safety: NO
!operator. Use?and flow analysis (e.g.if (x != null)). (link)
| 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; |
There was a problem hiding this comment.
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
- Null Safety: NO
!operator. Use?and flow analysis (e.g.if (x != null)). (link)
| JSPromise _rejectWith(JSAny error) => (globalContext['Promise']! as JSObject) | ||
| .callMethod<JSPromise>('reject'.toJS, error); |
There was a problem hiding this comment.
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
- Null Safety: NO
!operator. Use?and flow analysis (e.g.if (x != null)). (link)
| JSObject _firebaseAuthError() { | ||
| final error = (globalContext['Error']! as JSFunction) | ||
| .callAsConstructor<JSObject>( | ||
| 'Firebase: Error (auth/user-disabled).'.toJS, | ||
| ); |
There was a problem hiding this comment.
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
- Null Safety: NO
!operator. Use?and flow analysis (e.g.if (x != null)). (link)
| JSObject _jsError(String message, {String? code}) { | ||
| final error = (globalContext['Error']! as JSFunction) | ||
| .callAsConstructor<JSObject>(message.toJS); |
There was a problem hiding this comment.
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
- Null Safety: NO
!operator. Use?and flow analysis (e.g.if (x != null)). (link)
SelaseKay
left a comment
There was a problem hiding this comment.
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.
Keep the JS error check from #18923, and still store the emulator origin, throwing in debug when this instance is on production.
Description
Functions (web):
call(),stream()and the abort-signal helper cast every caught error toJSObject, so a Dart error became aTypeError. A JS rejection without a stringcodeormessage({}, a plainError, or theTypeErrorfrom a negativeTimeLimit) hit a null check instead of propagating.Auth (web):
guardAuthExceptions,getFirebaseAuthException,verifyPhoneNumberand the reCAPTCHA verifier'srender()/verify()cast caught errors toJSError, so a Dart error (for example one thrown bycodeSent) became aTypeError.useAuthEmulatorcheckede is AuthError, which is true for any JS object, then read a non-nullcode, so a JS error withoutcodecrashed.Fix: Dart errors are rethrown unchanged with their stack trace (the auth Future path uses
catchError(test:), likeguardWebExceptions). JS errors still go to the mappers, which readcode/messagenull-safely. Firebase errors are mapped exactly as before.Tests (
--platform chrome, and--wasm):cloud_functions_web/test/https_callable_web_test.dart:call()andstream()with a FunctionsError, a JS rejection without code or message, a plain JSError, 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 inget_firebase_auth_exception_test.dart. Therethrows a Dart error…tests andmaps a JS error without code to an unknown errorfail 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.yamland changelogs is not required.///).melos run analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?