Controllers should be able to handle errors themselves

This commit is contained in:
regiostech 2016-07-04 14:06:31 -04:00
parent 1a7d831a36
commit 76d26ea9c5
4 changed files with 90 additions and 84 deletions

View file

@ -52,7 +52,6 @@ class Controller {
ResponseContext res) async {
List args = [];
try {
// Load parameters, and execute
for (int i = 0; i < methodMirror.parameters.length; i++) {
ParameterMirror parameter = methodMirror.parameters[i];
@ -76,9 +75,6 @@ class Controller {
return await instanceMirror
.invoke(key, args)
.reflectee;
} catch (e) {
throw new AngelHttpException(e);
}
};
Route route = new Route(
exposeMirror.reflectee.method,

View file

@ -83,7 +83,7 @@ class ResponseContext extends Extensible {
<body>
<h1>Currently redirecting you...</h1>
<br />
Click <a href="$url"></a> if you are not automatically redirected...
Click <a href="$url">here</a> if you are not automatically redirected...
<script>
window.location = "$url";
</script>

View file

@ -12,6 +12,12 @@ typedef Future AngelConfigurer(Angel app);
/// A powerful real-time/REST/MVC server class.
class Angel extends Routable {
var _beforeProcessed = new StreamController<HttpRequest>();
var _afterProcessed = new StreamController<HttpRequest>();
Stream<HttpRequest> get beforeProcessed => _beforeProcessed.stream;
Stream<HttpRequest> get afterProcessed => _afterProcessed.stream;
ServerGenerator _serverGenerator =
(address, port) async => await HttpServer.bind(address, port);
@ -50,7 +56,13 @@ class Angel extends Routable {
await _serverGenerator(address ?? InternetAddress.LOOPBACK_IP_V4, port);
this.httpServer = server;
server.listen((HttpRequest request) async {
server.listen(handleRequest);
return server;
}
Future handleRequest(HttpRequest request) async {
_beforeProcessed.add(request);
String req_url =
request.uri.toString().replaceAll("?" + request.uri.query, "").replaceAll(new RegExp(r'\/+$'), '');
if (req_url.isEmpty) req_url = '/';
@ -108,9 +120,6 @@ class Angel extends Routable {
await execHandler(handler, req);
}
_finalizeResponse(request, res);
});
return server;
}
Future<bool> _applyHandler(
@ -162,6 +171,7 @@ class Angel extends Routable {
if (!res.willCloseItself) {
res.responseData.forEach((blob) => request.response.add(blob));
await request.response.close();
_afterProcessed.add(request);
}
} catch (e) {
// Remember: This fails silently

View file

@ -1,5 +1,5 @@
name: angel_framework
version: 1.0.0-dev.8
version: 1.0.0-dev.13
description: Core libraries for the Angel framework.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_framework