Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Unified Diff: pkg/shelf/lib/shelf_io.dart

Issue 252393007: Make sure handler errors won't bring down a shelf server. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/shelf/lib/shelf_io.dart
diff --git a/pkg/shelf/lib/shelf_io.dart b/pkg/shelf/lib/shelf_io.dart
index b8667190142fda1929e1dd76afc2a92abedb72e9..b5f146c7720e4c315df2314b06a1e29cf105c7ca 100644
--- a/pkg/shelf/lib/shelf_io.dart
+++ b/pkg/shelf/lib/shelf_io.dart
@@ -6,6 +6,8 @@
///
/// One can provide an instance of [HttpServer] as the `requests` parameter in
/// [serveRequests].
+///
+/// The `dart:io` adapter supports request hijacking; see [Request.hijack].
kevmoo 2014/04/25 13:13:06 Not yet?
nweiz 2014/04/25 18:18:13 Merge error, removed.
library shelf.io;
import 'dart:async';
@@ -34,8 +36,17 @@ Future<HttpServer> serve(Handler handler, address, int port,
///
/// [HttpServer] implements [Stream<HttpRequest>] so it can be passed directly
/// to [serveRequests].
+///
+/// Errors thrown by [handler] while serving a request will be printed to the
+/// console and cause a 500 response with no body. Errors thrown asynchronously
+/// by [handler] will be printed to the console or, if there's an active error
+/// zone, passed to that zone.
void serveRequests(Stream<HttpRequest> requests, Handler handler) {
- requests.listen((request) => handleRequest(request, handler));
+ catchTopLevelErrors(() {
+ requests.listen((request) => handleRequest(request, handler));
+ }, (error, stackTrace) {
+ _logError('Asynchronous error\n$error', stackTrace);
+ });
}
/// Uses [handler] to handle [request].
@@ -46,14 +57,7 @@ Future handleRequest(HttpRequest request, Handler handler) {
return syncFuture(() => handler(shelfRequest))
.catchError((error, stackTrace) {
- var chain = new Chain.current();
- if (stackTrace != null) {
- chain = new Chain.forTrace(stackTrace)
- .foldFrames((frame) => frame.isCore || frame.package == 'shelf')
- .terse;
- }
-
- return _logError('Error thrown by handler\n$error\n$chain');
+ return _logError('Error thrown by handler\n$error', stackTrace);
}).then((response) {
if (response == null) {
response = _logError('null response from handler');
@@ -95,8 +99,17 @@ Future _writeResponse(Response response, HttpResponse httpResponse) {
// TODO(kevmoo) A developer mode is needed to include error info in response
// TODO(kevmoo) Make error output plugable. stderr, logging, etc
-Response _logError(String message) {
+Response _logError(String message, [StackTrace stackTrace]) {
+ var chain = new Chain.current();
+ if (stackTrace != null) {
+ chain = new Chain.forTrace(stackTrace);
+ }
+ chain = chain
+ .foldFrames((frame) => frame.isCore || frame.package == 'shelf')
+ .terse;
+
stderr.writeln('ERROR - ${new DateTime.now()}');
stderr.writeln(message);
+ stderr.writeln(chain);
return new Response.internalServerError();
}
« pkg/shelf/README.md ('K') | « pkg/shelf/README.md ('k') | pkg/shelf/lib/src/util.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698