Skip to content

Error zone boundaries make it difficult to catch errors #26450

@BlackHC

Description

@BlackHC

I have a method runInApp(body()) that executes body in the (Angular) zone which is its own error zone.

runInApp(body()) => zone.run(body);

zone.run calls angularZone.runGuarded(body) internally.

If something throws in body, it never passes the error zone boundary. This is particularly annoying in tests because they end up timing out without telling you about the exception.

The only way I've found to route errors through the error zone boundary is not very beautiful:
(It has to support async and sync body()s.)

runInApp(body()) {
    var result;
    var error;
    var stackTrace;
    var completer = new Completer();
    zone.run(() {
      try {
        result = body();
        if (result is Future) {
          result.then((result) {
            completer.complete(result);
          }, onError: (e, st) {
            completer.completeError(e, st);
          });
          result = completer.future;
        }
      } catch (e,st) {
        error = e;
        stackTrace = st;
      }
    });
    if (error != null) {
      print(stackTrace);
      throw error;
    }
    return result;
  }

This feels rather cumbersome. Is there another way to handle this?

Also, there is no way to rethrow an exception in synchronous code that preserves the original stacktrace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.closed-obsoleteClosed as the reported issue is no longer relevantcore-nlibrary-async

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions