Index: utils/pub/command_lish.dart |
diff --git a/utils/pub/command_lish.dart b/utils/pub/command_lish.dart |
index df0f21d08b418196576f5537d1e664aa536f2541..b0f8733d73fb9b85bec564cbb219116d85b36227 100644 |
--- a/utils/pub/command_lish.dart |
+++ b/utils/pub/command_lish.dart |
@@ -12,6 +12,7 @@ import '../../pkg/args/lib/args.dart'; |
import '../../pkg/http/lib/http.dart' as http; |
import 'directory_tree.dart'; |
import 'git.dart' as git; |
+import 'http.dart'; |
import 'io.dart'; |
import 'log.dart' as log; |
import 'oauth2.dart' as oauth2; |
@@ -27,6 +28,8 @@ class LishCommand extends PubCommand { |
ArgParser get commandParser { |
var parser = new ArgParser(); |
+ // TODO(nweiz): Use HostedSource.defaultUrl as the default value once we use |
+ // dart:io for HTTPS requests. |
parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', |
help: 'The package server to which to upload this package'); |
return parser; |
@@ -42,17 +45,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 +67,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) { |
+ handleJsonError(e.response); |
} |
}); |
} |
@@ -192,31 +169,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. |