Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(403)

Unified Diff: utils/pub/io.dart

Issue 11569046: Add a pub command for managing uploaders for packages. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: utils/pub/io.dart
diff --git a/utils/pub/io.dart b/utils/pub/io.dart
index c7e3b04625a1a91b023e39bcd87561f5a8509b27..f18916874825d27b867335e01ca40db0d229993c 100644
--- a/utils/pub/io.dart
+++ b/utils/pub/io.dart
@@ -9,6 +9,7 @@ library io;
import 'dart:io';
import 'dart:isolate';
+import 'dart:json';
import 'dart:uri';
// TODO(nweiz): Make this import better.
@@ -565,6 +566,55 @@ final httpClient = new PubHttpClient();
final curlClient = new PubHttpClient(new CurlClient());
+/// Handles a successful JSON-formatted response from pub.dartlang.org.
Bob Nystrom 2012/12/17 18:41:58 io.dart is becoming a bit of a dumping ground. How
nweiz 2012/12/17 21:18:48 Done.
+///
+/// These responses are expected to be of the form `{"success": {"message":
+/// "some message"}}`. If the format is correct, the message will be printed;
+/// otherwise an error will be raised.
+void handleJsonSuccess(http.Response response) {
+ var parsed = parseJsonResponse(response);
+ if (parsed['success'] is! Map ||
+ !parsed['success'].containsKey('message') ||
+ parsed['success']['message'] is! String) {
+ invalidServerResponse(response);
+ }
+ log.message(parsed['success']['message']);
+}
+
+/// Handles an unsuccessful JSON-formatted response from pub.dartlang.org.
+///
+/// These responses are expected to be of the form `{"error": {"message": "some
+/// message"}}`. If the format is correct, the message will be raised as an
+/// error; otherwise an [invalidServerResponse] error will be raised.
+void handleJsonError(http.Response response) {
+ var errorMap = parseJsonResponse(response);
+ if (errorMap['error'] is! Map ||
+ !errorMap['error'].containsKey('message') ||
+ errorMap['error']['message'] is! String) {
+ invalidServerResponse(response);
+ }
+ throw errorMap['error']['message'];
+}
+
+/// 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 parseJsonResponse(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;
+}
+
+/// Throws an error describing an invalid response from the server.
+void invalidServerResponse(http.Response response) {
+ throw 'Invalid server response:\n${response.body}';
+}
+
/**
* Takes all input from [source] and writes it to [sink].
*

Powered by Google App Engine
This is Rietveld 408576698