Better errors
This commit is contained in:
parent
4ad4180f22
commit
462c4f4a79
3 changed files with 80 additions and 76 deletions
|
@ -1,6 +1,6 @@
|
|||
# angel_framework
|
||||
|
||||
[![pub 1.0.0-dev.45](https://img.shields.io/badge/pub-1.0.0--dev.45-red.svg)](https://pub.dartlang.org/packages/angel_framework)
|
||||
[![pub 1.0.0-dev.46](https://img.shields.io/badge/pub-1.0.0--dev.46-red.svg)](https://pub.dartlang.org/packages/angel_framework)
|
||||
[![build status](https://travis-ci.org/angel-dart/framework.svg)](https://travis-ci.org/angel-dart/framework)
|
||||
|
||||
Core libraries for the Angel Framework.
|
||||
|
|
|
@ -190,98 +190,105 @@ class Angel extends AngelBase {
|
|||
}
|
||||
|
||||
Future handleRequest(HttpRequest request) async {
|
||||
_beforeProcessed.add(request);
|
||||
try {
|
||||
_beforeProcessed.add(request);
|
||||
|
||||
final req = await RequestContext.from(request, this);
|
||||
final res = new ResponseContext(request.response, this);
|
||||
String requestedUrl = request.uri.path.replaceAll(_straySlashes, '');
|
||||
final req = await RequestContext.from(request, this);
|
||||
final res = new ResponseContext(request.response, this);
|
||||
String requestedUrl = request.uri.path.replaceAll(_straySlashes, '');
|
||||
|
||||
if (requestedUrl.isEmpty) requestedUrl = '/';
|
||||
if (requestedUrl.isEmpty) requestedUrl = '/';
|
||||
|
||||
final resolved =
|
||||
resolveAll(requestedUrl, requestedUrl, method: request.method);
|
||||
final resolved =
|
||||
resolveAll(requestedUrl, requestedUrl, method: request.method);
|
||||
|
||||
for (final result in resolved) req.params.addAll(result.allParams);
|
||||
for (final result in resolved) req.params.addAll(result.allParams);
|
||||
|
||||
if (resolved.isNotEmpty) {
|
||||
final route = resolved.first.route;
|
||||
req.inject(Match, route.match(requestedUrl));
|
||||
}
|
||||
if (resolved.isNotEmpty) {
|
||||
final route = resolved.first.route;
|
||||
req.inject(Match, route.match(requestedUrl));
|
||||
}
|
||||
|
||||
final m = new MiddlewarePipeline(resolved);
|
||||
req.inject(MiddlewarePipeline, m);
|
||||
final m = new MiddlewarePipeline(resolved);
|
||||
req.inject(MiddlewarePipeline, m);
|
||||
|
||||
final pipeline = []..addAll(before)..addAll(m.handlers)..addAll(after);
|
||||
final pipeline = []..addAll(before)..addAll(m.handlers)..addAll(after);
|
||||
|
||||
_printDebug('Handler sequence on $requestedUrl: $pipeline');
|
||||
_printDebug('Handler sequence on $requestedUrl: $pipeline');
|
||||
|
||||
for (final handler in pipeline) {
|
||||
try {
|
||||
_printDebug('Executing handler: $handler');
|
||||
final result = await executeHandler(handler, req, res);
|
||||
_printDebug('Result: $result');
|
||||
for (final handler in pipeline) {
|
||||
try {
|
||||
_printDebug('Executing handler: $handler');
|
||||
final result = await executeHandler(handler, req, res);
|
||||
_printDebug('Result: $result');
|
||||
|
||||
if (!result) {
|
||||
_printDebug('Last executed handler: $handler');
|
||||
break;
|
||||
} else {
|
||||
_printDebug(
|
||||
'Handler completed successfully, did not terminate response: $handler');
|
||||
}
|
||||
} catch (e, st) {
|
||||
_printDebug('Caught error in handler $handler: $e');
|
||||
_printDebug(st);
|
||||
if (!result) {
|
||||
_printDebug('Last executed handler: $handler');
|
||||
break;
|
||||
} else {
|
||||
_printDebug(
|
||||
'Handler completed successfully, did not terminate response: $handler');
|
||||
}
|
||||
} catch (e, st) {
|
||||
_printDebug('Caught error in handler $handler: $e');
|
||||
_printDebug(st);
|
||||
|
||||
if (e is AngelHttpException) {
|
||||
// Special handling for AngelHttpExceptions :)
|
||||
try {
|
||||
res.statusCode = e.statusCode;
|
||||
List<String> accept =
|
||||
request.headers[HttpHeaders.ACCEPT] ?? ['*/*'];
|
||||
if (accept.isEmpty ||
|
||||
accept.contains('*/*') ||
|
||||
accept.contains(ContentType.JSON.mimeType) ||
|
||||
accept.contains("application/javascript")) {
|
||||
res.serialize(e.toMap());
|
||||
} else {
|
||||
await _errorHandler(e, req, res);
|
||||
if (e is AngelHttpException) {
|
||||
// Special handling for AngelHttpExceptions :)
|
||||
try {
|
||||
res.statusCode = e.statusCode;
|
||||
List<String> accept =
|
||||
request.headers[HttpHeaders.ACCEPT] ?? ['*/*'];
|
||||
if (accept.isEmpty ||
|
||||
accept.contains('*/*') ||
|
||||
accept.contains(ContentType.JSON.mimeType) ||
|
||||
accept.contains("application/javascript")) {
|
||||
res.serialize(e.toMap());
|
||||
} else {
|
||||
await _errorHandler(e, req, res);
|
||||
}
|
||||
// _finalizeResponse(request, res);
|
||||
} catch (e, st) {
|
||||
_fatalErrorStream.add(
|
||||
new AngelFatalError(request: request, error: e, stack: st));
|
||||
}
|
||||
// _finalizeResponse(request, res);
|
||||
} catch (e, st) {
|
||||
} else {
|
||||
_fatalErrorStream.add(
|
||||
new AngelFatalError(request: request, error: e, stack: st));
|
||||
}
|
||||
} else {
|
||||
_fatalErrorStream
|
||||
.add(new AngelFatalError(request: request, error: e, stack: st));
|
||||
}
|
||||
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
_afterProcessed.add(request);
|
||||
try {
|
||||
_afterProcessed.add(request);
|
||||
|
||||
if (!res.willCloseItself) {
|
||||
for (var finalizer in responseFinalizers) {
|
||||
await finalizer(req, res);
|
||||
if (!res.willCloseItself) {
|
||||
for (var finalizer in responseFinalizers) {
|
||||
await finalizer(req, res);
|
||||
}
|
||||
|
||||
for (var key in res.headers.keys) {
|
||||
request.response.headers.set(key, res.headers[key]);
|
||||
}
|
||||
|
||||
request.response.headers.chunkedTransferEncoding =
|
||||
res.chunked ?? true;
|
||||
|
||||
request.response
|
||||
..statusCode = res.statusCode
|
||||
..cookies.addAll(res.cookies)
|
||||
..add(res.buffer.takeBytes());
|
||||
await request.response.close();
|
||||
}
|
||||
|
||||
for (var key in res.headers.keys) {
|
||||
request.response.headers.set(key, res.headers[key]);
|
||||
}
|
||||
|
||||
request.response.headers.chunkedTransferEncoding = res.chunked ?? true;
|
||||
|
||||
request.response
|
||||
..statusCode = res.statusCode
|
||||
..cookies.addAll(res.cookies)
|
||||
..add(res.buffer.takeBytes());
|
||||
await request.response.close();
|
||||
} catch (e, st) {
|
||||
_fatalErrorStream
|
||||
.add(new AngelFatalError(request: request, error: e, stack: st));
|
||||
}
|
||||
} catch (e) {
|
||||
failSilently(request, res);
|
||||
} catch (e, st) {
|
||||
_fatalErrorStream
|
||||
.add(new AngelFatalError(request: request, error: e, stack: st));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -335,9 +342,6 @@ class Angel extends AngelBase {
|
|||
_onController.add(controllers[configurer.findExpose().path] = configurer);
|
||||
}
|
||||
|
||||
/// Fallback when an error is thrown while handling a request.
|
||||
void failSilently(HttpRequest request, ResponseContext res) {}
|
||||
|
||||
/// Starts the server, wrapped in a [runZoned] call.
|
||||
void listen({InternetAddress address, int port: 3000}) {
|
||||
runZoned(() async {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: angel_framework
|
||||
version: 1.0.0-dev.45
|
||||
version: 1.0.0-dev.46
|
||||
description: Core libraries for the Angel framework.
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/angel_framework
|
||||
|
|
Loading…
Reference in a new issue