Chromium Code Reviews| Index: utils/pub/command_lish.dart |
| diff --git a/utils/pub/command_lish.dart b/utils/pub/command_lish.dart |
| index df0f21d08b418196576f5537d1e664aa536f2541..b310a549f73ed2f42c586ed1185667ec80ee8fcf 100644 |
| --- a/utils/pub/command_lish.dart |
| +++ b/utils/pub/command_lish.dart |
| @@ -42,17 +42,17 @@ class LishCommand extends PubCommand { |
| // should report that error and exit. |
| var newUri = server.resolve("/packages/versions/new.json"); |
| return client.get(newUri).chain((response) { |
| - var parameters = _parseJson(response); |
| + var parameters = parseJsonResponse(response); |
| var url = _expectField(parameters, 'url', response); |
| - if (url is! String) _invalidServerResponse(response); |
| + if (url is! String) invalidServerResponse(response); |
| cloudStorageUrl = new Uri.fromString(url); |
| var request = new http.MultipartRequest('POST', cloudStorageUrl); |
| var fields = _expectField(parameters, 'fields', response); |
| - if (fields is! Map) _invalidServerResponse(response); |
| + if (fields is! Map) invalidServerResponse(response); |
| fields.forEach((key, value) { |
| - if (value is! String) _invalidServerResponse(response); |
| + if (value is! String) invalidServerResponse(response); |
| request.fields[key] = value; |
| }); |
| @@ -64,44 +64,18 @@ class LishCommand extends PubCommand { |
| var location = response.headers['location']; |
| if (location == null) throw new PubHttpException(response); |
| return location; |
| - }).chain((location) => client.get(location)).transform((response) { |
| - var parsed = _parseJson(response); |
| - if (parsed['success'] is! Map || |
| - !parsed['success'].containsKey('message') || |
| - parsed['success']['message'] is! String) { |
| - _invalidServerResponse(response); |
| - } |
| - log.message(parsed['success']['message']); |
| - }); |
| + }).chain((location) => client.get(location)) |
| + .transform(handleJsonSuccess); |
| }).transformException((e) { |
| - if (e is PubHttpException) { |
| - var url = e.response.request.url; |
| - if (url.toString() == cloudStorageUrl.toString()) { |
| - // TODO(nweiz): the response may have XML-formatted information about |
| - // the error. Try to parse that out once we have an easily-accessible |
| - // XML parser. |
| - throw 'Failed to upload the package.'; |
| - } else if (url.origin == server.origin) { |
| - var errorMap = _parseJson(e.response); |
| - if (errorMap['error'] is! Map || |
| - !errorMap['error'].containsKey('message') || |
| - errorMap['error']['message'] is! String) { |
| - _invalidServerResponse(e.response); |
| - } |
| - throw errorMap['error']['message']; |
| - } |
| - } else if (e is oauth2.ExpirationException) { |
| - log.error("Pub's authorization to upload packages has expired and " |
| - "can't be automatically refreshed."); |
| - return _publish(packageBytes); |
| - } else if (e is oauth2.AuthorizationException) { |
| - var message = "OAuth2 authorization failed"; |
| - if (e.description != null) message = "$message (${e.description})"; |
| - log.error("$message."); |
| - return oauth2.clearCredentials(cache).chain((_) => |
| - _publish(packageBytes)); |
| - } else { |
| - throw e; |
| + if (e is! PubHttpException) throw e; |
| + var url = e.response.request.url; |
| + if (url.toString() == cloudStorageUrl.toString()) { |
| + // TODO(nweiz): the response may have XML-formatted information about |
| + // the error. Try to parse that out once we have an easily-accessible |
| + // XML parser. |
| + throw 'Failed to upload the package.'; |
| + } else if (url.origin == server.origin) { |
|
Bob Nystrom
2012/12/17 18:41:58
What is this check for?
nweiz
2012/12/17 21:18:48
handleJsonError checks for an error in a format sp
|
| + handleJsonError(e.response); |
| } |
| }); |
| } |
| @@ -192,31 +166,11 @@ class LishCommand extends PubCommand { |
| })); |
| } |
| - /// Parses a response body, assuming it's JSON-formatted. Throws a |
| - /// user-friendly error if the response body is invalid JSON, or if it's not a |
| - /// map. |
| - Map _parseJson(http.Response response) { |
| - var value; |
| - try { |
| - value = JSON.parse(response.body); |
| - } catch (e) { |
| - // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. |
| - _invalidServerResponse(response); |
| - } |
| - if (value is! Map) _invalidServerResponse(response); |
| - return value; |
| - } |
| - |
| /// Returns the value associated with [key] in [map]. Throws a user-friendly |
| /// error if [map] doens't contain [key]. |
| _expectField(Map map, String key, http.Response response) { |
| if (map.containsKey(key)) return map[key]; |
| - _invalidServerResponse(response); |
| - } |
| - |
| - /// Throws an error describing an invalid response from the server. |
| - void _invalidServerResponse(http.Response response) { |
| - throw 'Invalid server response:\n${response.body}'; |
| + invalidServerResponse(response); |
| } |
| /// Validates the package. Throws an exception if it's invalid. |