firebase - 如何打印有意义的 firebase-dart 异常

标签 firebase exception flutter dart

我无法将有意义的异常错误打印到控制台。我只得到 [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/

相关文章:

flutter_gauge 小部件不会更新 setState 上的索引值

firebase - 如何在 Flutter 中的 firebase 查询中传递多个条件

ios - Swift:我应该为 Firebase 管理器类使用静态成员吗​​?好的程序设计

swift - 如何限制管理员对 firebase firestore 数据库的读取访问?

list - 如何遍历列表并在Dart中重置?

java - 无法 Autowiring 字段 : Spring-Hibernate

JavaScript 压缩器输出换行而不是分号 - 基于 Windows

java - 等待图片下载完毕 Firebase

swift - 检查文档是否存在于 Firestore 交易中

windows - 什么是 "unknown software exception (0xc00000fd)"错误以及如何避免它?