Skip to content

Commit f9f46a5

Browse files
authored
fix(database, web): Fix exceptions being converted into lowercase (#12661)
* fix(database, web): Fix exception being converted into lowercase * add tests for exceptions * add LICENSE * remove unnecessary line * fix * fix
1 parent 6be9597 commit f9f46a5

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

packages/firebase_database/firebase_database_web/lib/src/utils/exception.dart

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,26 @@ FirebaseException convertFirebaseDatabaseException(Object exception,
1010
[StackTrace? stackTrace]) {
1111
final castedJSObject = exception as core_interop.JSError;
1212
String code = 'unknown';
13-
String message = castedJSObject.message?.toDart.toLowerCase() ?? '';
13+
String message = castedJSObject.message?.toDart ?? '';
14+
String lowerCaseMessage = message.toLowerCase();
1415

1516
// FirebaseWeb SDK for Database has no error codes, so we manually map known
1617
// messages to known error codes for cross platform consistency.
17-
if (message.contains('index not defined')) {
18+
if (lowerCaseMessage.contains('index not defined')) {
1819
code = 'index-not-defined';
19-
} else if (message.contains('permission denied') ||
20-
message.contains('permission_denied')) {
20+
} else if (lowerCaseMessage.contains('permission denied') ||
21+
lowerCaseMessage.contains('permission_denied')) {
2122
code = 'permission-denied';
22-
} else if (message
23+
} else if (lowerCaseMessage
2324
.contains('transaction needs to be run again with current data')) {
2425
code = 'data-stale';
25-
} else if (message.contains('transaction had too many retries')) {
26+
} else if (lowerCaseMessage.contains('transaction had too many retries')) {
2627
code = 'max-retries';
27-
} else if (message.contains('service is unavailable')) {
28+
} else if (lowerCaseMessage.contains('service is unavailable')) {
2829
code = 'unavailable';
29-
} else if (message.contains('network error')) {
30+
} else if (lowerCaseMessage.contains('network error')) {
3031
code = 'network-error';
31-
} else if (message.contains('write was canceled')) {
32+
} else if (lowerCaseMessage.contains('write was canceled')) {
3233
code = 'write-cancelled';
3334
}
3435

tests/integration_test/firebase_database/firebase_database_e2e_test.dart

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:tests/firebase_options.dart';
1111
import 'data_snapshot_e2e.dart';
1212
import 'database_e2e.dart';
1313
import 'database_reference_e2e.dart';
14+
import 'web_only_stub.dart' if (dart.library.js_interop) 'web_only.dart';
1415
import 'firebase_database_configuration_e2e.dart';
1516
import 'query_e2e.dart';
1617

@@ -42,6 +43,7 @@ void main() {
4243
setupDatabaseReferenceTests();
4344
setupQueryTests();
4445
setupDataSnapshotTests();
46+
setupWebOnlyTests();
4547
// TODO(ehesp): Fix broken tests
4648
// runOnDisconnectTests();
4749
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2021 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:js_interop';
6+
7+
import 'package:flutter/foundation.dart';
8+
import 'package:flutter_test/flutter_test.dart';
9+
10+
import 'package:firebase_database_web/firebase_database_web.dart';
11+
12+
void setupWebOnlyTests() {
13+
group(
14+
'web',
15+
() {
16+
test('convertFirebaseDatabaseException', () {
17+
Object jsErr(String? message) {
18+
return {
19+
'message': message,
20+
}.jsify()! as Object;
21+
}
22+
23+
final cases = [
24+
['Capital small', 'unknown'],
25+
[null, 'unknown'],
26+
['Index not defined', 'index-not-defined'],
27+
];
28+
29+
for (var i = 0; i < cases.length; i++) {
30+
final message = cases[i][0];
31+
final convertedCode = cases[i][1];
32+
var converted = convertFirebaseDatabaseException(jsErr(message));
33+
34+
expect(
35+
converted.message,
36+
message ?? '',
37+
reason: '[$i] Failed message check',
38+
);
39+
expect(
40+
converted.code,
41+
convertedCode,
42+
reason: '[$i] Failed code check',
43+
);
44+
expect(
45+
converted.plugin,
46+
'firebase_database',
47+
reason: '[$i] Failed plugin check',
48+
);
49+
}
50+
});
51+
},
52+
skip: !kIsWeb,
53+
);
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2021 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Used for non web platforms
6+
void setupWebOnlyTests() {
7+
}

0 commit comments

Comments
 (0)