From f8bca542c4242d226574570b9bedde56cbbb6bfd Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 20 Aug 2018 21:12:12 -0400 Subject: [PATCH] Catch sync errors when using zone --- example/main.dart | 10 ++++++++++ lib/src/http/angel_http.dart | 14 +++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/example/main.dart b/example/main.dart index 8ffa35b7..3a97ef61 100644 --- a/example/main.dart +++ b/example/main.dart @@ -1,11 +1,21 @@ import 'package:angel_container/mirrors.dart'; import 'package:angel_framework/angel_framework.dart'; +import 'package:logging/logging.dart'; main() async { var app = new Angel( + logger: Logger('angel'), reflector: MirrorsReflector(), ); + hierarchicalLoggingEnabled = true; + + app.logger.onRecord.listen((rec) { + print(rec); + if (rec.error != null) print(rec.error); + if (rec.stackTrace != null) print(rec.stackTrace); + }); + // Index route. Returns JSON. app.get('/', (req, res) => res.write('Welcome to Angel!')); diff --git a/lib/src/http/angel_http.dart b/lib/src/http/angel_http.dart index d3055c31..69dd6eb4 100644 --- a/lib/src/http/angel_http.dart +++ b/lib/src/http/angel_http.dart @@ -227,9 +227,17 @@ class AngelHttp { req.container.registerSingleton(zone); req.container.registerSingleton(zoneSpec); - return zone.run(handle).whenComplete(() { - res.dispose(); - }); + // If a synchronous error is thrown, it's not caught by `zone.run`, + // so use a try/catch, and recover when need be. + + try { + return zone.run(handle).whenComplete(() { + res.dispose(); + }); + } catch (e, st) { + zone.handleUncaughtError(e, st); + return Future.value(); + } } }); });