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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 } | 197 } |
198 throw errorMap['error']['message']; | 198 throw errorMap['error']['message']; |
199 } | 199 } |
200 | 200 |
201 /// Parses a response body, assuming it's JSON-formatted. Throws a user-friendly | 201 /// Parses a response body, assuming it's JSON-formatted. Throws a user-friendly |
202 /// error if the response body is invalid JSON, or if it's not a map. | 202 /// error if the response body is invalid JSON, or if it's not a map. |
203 Map parseJsonResponse(http.Response response) { | 203 Map parseJsonResponse(http.Response response) { |
204 var value; | 204 var value; |
205 try { | 205 try { |
206 value = json.parse(response.body); | 206 value = json.parse(response.body); |
207 } catch (e) { | 207 } on FormatException catch (e) { |
208 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. | |
209 invalidServerResponse(response); | 208 invalidServerResponse(response); |
210 } | 209 } |
211 if (value is! Map) invalidServerResponse(response); | 210 if (value is! Map) invalidServerResponse(response); |
212 return value; | 211 return value; |
213 } | 212 } |
214 | 213 |
215 /// Throws an error describing an invalid response from the server. | 214 /// Throws an error describing an invalid response from the server. |
216 void invalidServerResponse(http.Response response) => | 215 void invalidServerResponse(http.Response response) => |
217 fail('Invalid server response:\n${response.body}'); | 216 fail('Invalid server response:\n${response.body}'); |
218 | 217 |
219 /// Exception thrown when an HTTP operation fails. | 218 /// Exception thrown when an HTTP operation fails. |
220 class PubHttpException implements Exception { | 219 class PubHttpException implements Exception { |
221 final http.Response response; | 220 final http.Response response; |
222 | 221 |
223 const PubHttpException(this.response); | 222 const PubHttpException(this.response); |
224 | 223 |
225 String toString() => 'HTTP error ${response.statusCode}: ' | 224 String toString() => 'HTTP error ${response.statusCode}: ' |
226 '${response.reasonPhrase}'; | 225 '${response.reasonPhrase}'; |
227 } | 226 } |
OLD | NEW |