Catch sync errors when using zone
This commit is contained in:
parent
340b08da1b
commit
f8bca542c4
2 changed files with 21 additions and 3 deletions
|
@ -1,11 +1,21 @@
|
||||||
import 'package:angel_container/mirrors.dart';
|
import 'package:angel_container/mirrors.dart';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
main() async {
|
main() async {
|
||||||
var app = new Angel(
|
var app = new Angel(
|
||||||
|
logger: Logger('angel'),
|
||||||
reflector: MirrorsReflector(),
|
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.
|
// Index route. Returns JSON.
|
||||||
app.get('/', (req, res) => res.write('Welcome to Angel!'));
|
app.get('/', (req, res) => res.write('Welcome to Angel!'));
|
||||||
|
|
||||||
|
|
|
@ -227,9 +227,17 @@ class AngelHttp {
|
||||||
req.container.registerSingleton<Zone>(zone);
|
req.container.registerSingleton<Zone>(zone);
|
||||||
req.container.registerSingleton<ZoneSpecification>(zoneSpec);
|
req.container.registerSingleton<ZoneSpecification>(zoneSpec);
|
||||||
|
|
||||||
return zone.run(handle).whenComplete(() {
|
// If a synchronous error is thrown, it's not caught by `zone.run`,
|
||||||
res.dispose();
|
// 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue