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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /// A Shelf adapter for handling [HttpRequest] objects from `dart:io`. 5 /// A Shelf adapter for handling [HttpRequest] objects from `dart:io`.
6 /// 6 ///
7 /// One can provide an instance of [HttpServer] as the `requests` parameter in 7 /// One can provide an instance of [HttpServer] as the `requests` parameter in
8 /// [serveRequests]. 8 /// [serveRequests].
9 ///
10 /// 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.
9 library shelf.io; 11 library shelf.io;
10 12
11 import 'dart:async'; 13 import 'dart:async';
12 import 'dart:io'; 14 import 'dart:io';
13 15
14 import 'package:stack_trace/stack_trace.dart'; 16 import 'package:stack_trace/stack_trace.dart';
15 17
16 import 'shelf.dart'; 18 import 'shelf.dart';
17 import 'src/util.dart'; 19 import 'src/util.dart';
18 20
19 /// Starts an [HttpServer] that listens on the specified [address] and 21 /// Starts an [HttpServer] that listens on the specified [address] and
20 /// [port] and sends requests to [handler]. 22 /// [port] and sends requests to [handler].
21 /// 23 ///
22 /// See the documentation for [HttpServer.bind] for more details on [address], 24 /// See the documentation for [HttpServer.bind] for more details on [address],
23 /// [port], and [backlog]. 25 /// [port], and [backlog].
24 Future<HttpServer> serve(Handler handler, address, int port, 26 Future<HttpServer> serve(Handler handler, address, int port,
25 {int backlog}) { 27 {int backlog}) {
26 if (backlog == null) backlog = 0; 28 if (backlog == null) backlog = 0;
27 return HttpServer.bind(address, port, backlog: backlog).then((server) { 29 return HttpServer.bind(address, port, backlog: backlog).then((server) {
28 serveRequests(server, handler); 30 serveRequests(server, handler);
29 return server; 31 return server;
30 }); 32 });
31 } 33 }
32 34
33 /// Serve a [Stream] of [HttpRequest]s. 35 /// Serve a [Stream] of [HttpRequest]s.
34 /// 36 ///
35 /// [HttpServer] implements [Stream<HttpRequest>] so it can be passed directly 37 /// [HttpServer] implements [Stream<HttpRequest>] so it can be passed directly
36 /// to [serveRequests]. 38 /// to [serveRequests].
39 ///
40 /// Errors thrown by [handler] while serving a request will be printed to the
41 /// console and cause a 500 response with no body. Errors thrown asynchronously
42 /// by [handler] will be printed to the console or, if there's an active error
43 /// zone, passed to that zone.
37 void serveRequests(Stream<HttpRequest> requests, Handler handler) { 44 void serveRequests(Stream<HttpRequest> requests, Handler handler) {
38 requests.listen((request) => handleRequest(request, handler)); 45 catchTopLevelErrors(() {
46 requests.listen((request) => handleRequest(request, handler));
47 }, (error, stackTrace) {
48 _logError('Asynchronous error\n$error', stackTrace);
49 });
39 } 50 }
40 51
41 /// Uses [handler] to handle [request]. 52 /// Uses [handler] to handle [request].
42 /// 53 ///
43 /// Returns a [Future] which completes when the request has been handled. 54 /// Returns a [Future] which completes when the request has been handled.
44 Future handleRequest(HttpRequest request, Handler handler) { 55 Future handleRequest(HttpRequest request, Handler handler) {
45 var shelfRequest = _fromHttpRequest(request); 56 var shelfRequest = _fromHttpRequest(request);
46 57
47 return syncFuture(() => handler(shelfRequest)) 58 return syncFuture(() => handler(shelfRequest))
48 .catchError((error, stackTrace) { 59 .catchError((error, stackTrace) {
49 var chain = new Chain.current(); 60 return _logError('Error thrown by handler\n$error', stackTrace);
50 if (stackTrace != null) {
51 chain = new Chain.forTrace(stackTrace)
52 .foldFrames((frame) => frame.isCore || frame.package == 'shelf')
53 .terse;
54 }
55
56 return _logError('Error thrown by handler\n$error\n$chain');
57 }).then((response) { 61 }).then((response) {
58 if (response == null) { 62 if (response == null) {
59 response = _logError('null response from handler'); 63 response = _logError('null response from handler');
60 } 64 }
61 65
62 return _writeResponse(response, request.response); 66 return _writeResponse(response, request.response);
63 }); 67 });
64 } 68 }
65 69
66 /// Creates a new [Request] from the provided [HttpRequest]. 70 /// Creates a new [Request] from the provided [HttpRequest].
(...skipping 21 matching lines...) Expand all
88 if (response.headers[HttpHeaders.SERVER] == null) { 92 if (response.headers[HttpHeaders.SERVER] == null) {
89 var value = httpResponse.headers.value(HttpHeaders.SERVER); 93 var value = httpResponse.headers.value(HttpHeaders.SERVER);
90 httpResponse.headers.set(HttpHeaders.SERVER, '$value with Shelf'); 94 httpResponse.headers.set(HttpHeaders.SERVER, '$value with Shelf');
91 } 95 }
92 return httpResponse.addStream(response.read()) 96 return httpResponse.addStream(response.read())
93 .then((_) => httpResponse.close()); 97 .then((_) => httpResponse.close());
94 } 98 }
95 99
96 // TODO(kevmoo) A developer mode is needed to include error info in response 100 // TODO(kevmoo) A developer mode is needed to include error info in response
97 // TODO(kevmoo) Make error output plugable. stderr, logging, etc 101 // TODO(kevmoo) Make error output plugable. stderr, logging, etc
98 Response _logError(String message) { 102 Response _logError(String message, [StackTrace stackTrace]) {
103 var chain = new Chain.current();
104 if (stackTrace != null) {
105 chain = new Chain.forTrace(stackTrace);
106 }
107 chain = chain
108 .foldFrames((frame) => frame.isCore || frame.package == 'shelf')
109 .terse;
110
99 stderr.writeln('ERROR - ${new DateTime.now()}'); 111 stderr.writeln('ERROR - ${new DateTime.now()}');
100 stderr.writeln(message); 112 stderr.writeln(message);
113 stderr.writeln(chain);
101 return new Response.internalServerError(); 114 return new Response.internalServerError();
102 } 115 }
OLDNEW
« 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