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 throw new UserFacingException('Could not resolve URL ' |
| 73 '"${request.url.origin}".'); |
73 } else if (error.osError.errorCode == -12276) { | 74 } else if (error.osError.errorCode == -12276) { |
74 throw 'Unable to validate SSL certificate for ' | 75 throw new UserFacingException('Unable to validate SSL certificate ' |
75 '"${request.url.origin}".'; | 76 'for "${request.url.origin}".'); |
76 } | 77 } |
77 } | 78 } |
78 throw error; | 79 throw error; |
79 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); | 80 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); |
80 } | 81 } |
81 | 82 |
82 /// Logs the fact that [request] was sent, and information about it. | 83 /// Logs the fact that [request] was sent, and information about it. |
83 void _logRequest(http.BaseRequest request) { | 84 void _logRequest(http.BaseRequest request) { |
84 var requestLog = new StringBuffer(); | 85 var requestLog = new StringBuffer(); |
85 requestLog.writeln("HTTP ${request.method} ${request.url}"); | 86 requestLog.writeln("HTTP ${request.method} ${request.url}"); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 } catch (e) { | 182 } catch (e) { |
182 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. | 183 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. |
183 invalidServerResponse(response); | 184 invalidServerResponse(response); |
184 } | 185 } |
185 if (value is! Map) invalidServerResponse(response); | 186 if (value is! Map) invalidServerResponse(response); |
186 return value; | 187 return value; |
187 } | 188 } |
188 | 189 |
189 /// Throws an error describing an invalid response from the server. | 190 /// Throws an error describing an invalid response from the server. |
190 void invalidServerResponse(http.Response response) { | 191 void invalidServerResponse(http.Response response) { |
191 throw 'Invalid server response:\n${response.body}'; | 192 throw new UserFacingException('Invalid server response:\n${response.body}'); |
192 } | 193 } |
193 | 194 |
194 /// Exception thrown when an HTTP operation fails. | 195 /// Exception thrown when an HTTP operation fails. |
195 class PubHttpException implements Exception { | 196 class PubHttpException implements Exception { |
196 final http.Response response; | 197 final http.Response response; |
197 | 198 |
198 const PubHttpException(this.response); | 199 const PubHttpException(this.response); |
199 | 200 |
200 String toString() => 'HTTP error ${response.statusCode}: ' | 201 String toString() => 'HTTP error ${response.statusCode}: ' |
201 '${response.reasonPhrase}'; | 202 '${response.reasonPhrase}'; |
202 } | 203 } |
OLD | NEW |