我无法将有意义的异常错误打印到控制台。我只得到 [object Object]
@TestOn('browser')
import 'dart:io';
import 'package:firebase/firebase.dart';
import 'package:test/test.dart';
import 'package:workshop_participant_flutter/firebase_config.dart' as my_config;
void main() {
test('too big images cant be uploaded', () async {
App app;
app = my_config.initFirebase();
final ref = app.storage().ref('/storageTest/test.file');
final image = File('./too_big_file.png');
try {
await ref.put(image).future;
} on IOException catch (e) {
print(e);
} catch (e) {
print(e.toString());
print(e);
}
});
}
我正在将图像加载到 Firebase 存储,但它失败了,因为我已设置规则拒绝所有大于 x 字节的文件。这是想要的行为。
所有其他测试工作正常。所以设置和配置没问题。我现在的问题是,我想了解我收到的错误消息。我想捕获那个异常并处理它。
我希望有人偶然发现了同样的现象并可以在这里给我建议。
如果你想运行上面的测试代码,你还需要这个文件
<!DOCTYPE html>
<html lang="en">
<head>
<title>Upload Test</title>
</head>
<body>
<script src="https://www.gstatic.com/firebasejs/5.5.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.5.2/firebase-storage.js"></script>
<script src="packages/test/dart.js"></script>
<link rel="x-dart-test" href="upload_test.dart" />
</body>
</html>
最佳答案
您必须根据需要创建自定义异常。自定义异常示例。
class AmountException implements Exception {
String get errorMessage => 'Amount should be greater than zero';
}
void main() {
try {
withdrawAmount(-1);
} catch(e) {
print(e.errorMessage);
} finally {
print('Ending requested operation.....');
}
}
void withdrawAmount(int amount) {
if (amount <= 0) {
throw AmountException();
}
}
https://www.tutorialspoint.com/dart_programming/dart_programming_exceptions.htm
关于firebase - 如何打印有意义的 firebase-dart 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56358822/