dart - 为什么 onError 处理程序不在 Http.call() future 触发?

标签 dart angular-dart dart-async

我正在尝试使用 Http 服务发出 Web 请求。请求失败(如预期的那样)并返回 403。我希望 onError 处理程序会执行,但事实并非如此。异常被抛出并且从未被捕获。

_http(url: '/widgets/shiny-trinket', method: 'PUT', data: 'some malformed data')
  .then((resp) => successHandler, 
  onError: (resp) {
    // why aren't we getting here?
    displayErrorMessage();
  });

抛出的异常(从控制台):

Failed to load resource: the server responded with a status of 403 (Forbidden)
  http://localhost/widgets/shiny-trinket
Instance of '_XMLHttpRequestProgressEvent'

STACKTRACE:
null

我做错了什么吗?或者这是一个错误?

更新

我也试过下面的方法,还是不行。

_http(url: '/widgets/shiny-trinket', method: 'PUT', data: 'some malformed data')
  .then((resp) => successHandler) 
  .catchError((e) {
    // why aren't we getting here?
    displayErrorMessage();
  });

最佳答案

我刚刚尝试了问题 How do I get Geolocation API to work in Dartium? 中的示例这会产生 403 错误并打印 catchError 中的错误消息。

window.navigator
  .geolocation.getCurrentPosition()
    ..then((pos) {

      window.alert(pos.toString());
    })
    ..catchError((PositionError error) {
      window.alert("Error code: ${error.code}, Error message: ${error.message}");
    })
    ..whenComplete(() => window.alert("complete"))
    ;

关于dart - 为什么 onError 处理程序不在 Http.call() future 触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20822582/

相关文章:

flutter - 函数不会从 initState() 中执行

dart - Dart中的固定宽度格式

asynchronous - 为什么在以下程序中在同步之前调用异步功能?

Flutter 和 Angular Dart 都共享组件模式,但是这两个框架之间的一个根本区别是什么?

dart - 角 Dart 组件初始化

ajax - 尝试测试异步Dart Ajax HttpRequest时出错

json - 在 flutter 中从 API 获取数据时获取错误类型'_InternalLinkedHashMap<String, dynamic >' is not a subtype of type ' Iterable<dynamic>'

dart - AngularDart 中的模板中是否有可用的预定义变量?

dart - 为什么 Dart StreamController.addStream 在加入流时无法正常工作

dart - 等待流脉冲x时间,然后继续