OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /// Helpers for dealing with HTTP. | 5 /// Helpers for dealing with HTTP. |
6 library pub.http; | 6 library pub.http; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:json' as json; | 10 import 'dart:json' as json; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 throw new PubHttpException(response); | 62 throw new PubHttpException(response); |
63 }); | 63 }); |
64 }).catchError((error) { | 64 }).catchError((error) { |
65 if (error is SocketIOException && | 65 if (error is SocketIOException && |
66 error.osError != null) { | 66 error.osError != null) { |
67 if (error.osError.errorCode == 8 || | 67 if (error.osError.errorCode == 8 || |
68 error.osError.errorCode == -2 || | 68 error.osError.errorCode == -2 || |
69 error.osError.errorCode == -5 || | 69 error.osError.errorCode == -5 || |
70 error.osError.errorCode == 11001 || | 70 error.osError.errorCode == 11001 || |
71 error.osError.errorCode == 11004) { | 71 error.osError.errorCode == 11004) { |
72 throw 'Could not resolve URL "${request.url.origin}".'; | 72 fail('Could not resolve URL "${request.url.origin}".'); |
73 } else if (error.osError.errorCode == -12276) { | 73 } else if (error.osError.errorCode == -12276) { |
74 throw 'Unable to validate SSL certificate for ' | 74 fail('Unable to validate SSL certificate for ' |
75 '"${request.url.origin}".'; | 75 '"${request.url.origin}".'); |
76 } | 76 } |
77 } | 77 } |
78 throw error; | 78 throw error; |
79 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); | 79 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); |
80 } | 80 } |
81 | 81 |
82 /// Logs the fact that [request] was sent, and information about it. | 82 /// Logs the fact that [request] was sent, and information about it. |
83 void _logRequest(http.BaseRequest request) { | 83 void _logRequest(http.BaseRequest request) { |
84 var requestLog = new StringBuffer(); | 84 var requestLog = new StringBuffer(); |
85 requestLog.writeln("HTTP ${request.method} ${request.url}"); | 85 requestLog.writeln("HTTP ${request.method} ${request.url}"); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 value = json.parse(response.body); | 180 value = json.parse(response.body); |
181 } catch (e) { | 181 } catch (e) { |
182 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. | 182 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. |
183 invalidServerResponse(response); | 183 invalidServerResponse(response); |
184 } | 184 } |
185 if (value is! Map) invalidServerResponse(response); | 185 if (value is! Map) invalidServerResponse(response); |
186 return value; | 186 return value; |
187 } | 187 } |
188 | 188 |
189 /// Throws an error describing an invalid response from the server. | 189 /// Throws an error describing an invalid response from the server. |
190 void invalidServerResponse(http.Response response) { | 190 void invalidServerResponse(http.Response response) => |
191 throw 'Invalid server response:\n${response.body}'; | 191 fail('Invalid server response:\n${response.body}'); |
192 } | |
193 | 192 |
194 /// Exception thrown when an HTTP operation fails. | 193 /// Exception thrown when an HTTP operation fails. |
195 class PubHttpException implements Exception { | 194 class PubHttpException implements Exception { |
196 final http.Response response; | 195 final http.Response response; |
197 | 196 |
198 const PubHttpException(this.response); | 197 const PubHttpException(this.response); |
199 | 198 |
200 String toString() => 'HTTP error ${response.statusCode}: ' | 199 String toString() => 'HTTP error ${response.statusCode}: ' |
201 '${response.reasonPhrase}'; | 200 '${response.reasonPhrase}'; |
202 } | 201 } |
OLD | NEW |