如何处理集团的错误?

发布于 2025-02-04 10:29:13 字数 805 浏览 3 评论 0原文

我看到人们使用onerror仅进行调试。我以为我可以使用它来发出新状态,例如emit(errorState(消息:“ arror”))。但是有了 bloc软件包的新版本,我们应该使用提供给处理者的发射机,并且不应使用专用函数直接发射

目前,我在所有活动处理程序中都有尝试/捕获块。如果发生任何错误,我会用消息发出errorState,并用窗口小部件在UI中显示它。这是我应该如何处理错误的?这使事件处理程序的功能使这些尝试/捕获量看起来很糟糕。我想知道我是否正确地做到了,想知道它应该如何完成?

  void _startExercise(ExerciseStarted event, Emitter<ExerciseState> emit) async {
    emit(ExerciseLoadingState());
    try {
      final something = await _repository.doSomething();
      emit(ExerciseLoadedState(something: something));
    } catch (e) {
      log(e.toString());
      emit(const ExerciseErrorState());
    }
  }

I see people use onError just for debugging. I thought I could use it to emit new states like emit(ErrorState(message: "An error")). But with the new versions of the bloc package, we should use emitters provided to the handlers and shouldn't use the dedicated function emit directly.

Currently I have try/catch blocks in all of my event handlers. If any error occurs, I emit the ErrorState with a message and show it in the UI with a widget. Is this how should I handle errors? This makes event handler functions to look awful with these try/catchs. I wonder if I'm doing it correct and want to know how should it be done actually?

  void _startExercise(ExerciseStarted event, Emitter<ExerciseState> emit) async {
    emit(ExerciseLoadingState());
    try {
      final something = await _repository.doSomething();
      emit(ExerciseLoadedState(something: something));
    } catch (e) {
      log(e.toString());
      emit(const ExerciseErrorState());
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

并安 2025-02-11 10:29:13

好吧,让我们将此问题分为两个部分:

  1. 您会遇到错误,并且想散发/更改状态。

在这种情况下,您提供的代码示例正在正确执行 - 如果发生错误/异常,则必须使用尝试/捕获并发出错误状态。我唯一要调整的是将catch(e){...}更改为 在异常捕获(e){...}上在您的代码中处理,您可能会错过它们,以防万一您在那里抓住一切。

  1. 您会遇到错误,只想处理它。

在这种情况下,您可以处理存储库中的错误。例如,使用存储库中的try/catch块并记录错误。由于您不需要发出状态更改,因此将处理该错误,并且您的活动处理程序不会对此额外的尝试/捕获逻辑感到困惑。

无论如何,无论您选择哪种方式,只要保持一致,就应该很好。

Well, let's separate this problem into two parts:

  1. You get an error and you want to emit/change the state.

In this case, your provided code example is doing it properly - you have to use try/catch and emit an error state in case of an Error/Exception. The only thing I would adjust there is to change catch (e) {...} to on Exception catch (e) {...} since Dart errors must be handled in your code and you could simply miss them in case you catch everything there.

  1. You get an error and just want to handle it.

In this case, you can handle errors inside the repository. E.g. use the try/catch block inside the repository and log the error. Since you do not need to emit a state change, the error will be handled and also your event handler won't be cluttered with this extra try/catch logic.

Anyway, whichever way you choose, just be consistent and you should be good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文