Handle broken response from userinfo_endpoint (#3427)
diff --git a/lib/src/command/login.dart b/lib/src/command/login.dart
index 8c31e69..2f1f9f1 100644
--- a/lib/src/command/login.dart
+++ b/lib/src/command/login.dart
@@ -6,6 +6,7 @@
import 'dart:convert';
import '../command.dart';
+import '../command_runner.dart';
import '../http.dart';
import '../log.dart' as log;
import '../oauth2.dart' as oauth2;
@@ -26,12 +27,17 @@
final credentials = oauth2.loadCredentials(cache);
if (credentials == null) {
final userInfo = await _retrieveUserInfo();
- log.message('You are now logged in as $userInfo');
+ if (userInfo == null) {
+ log.warning('Could not retrieve your user-details.\n'
+ 'You might have to run `$topLevelProgram pub logout` to delete your credentials and try again.');
+ } else {
+ log.message('You are now logged in as $userInfo');
+ }
} else {
final userInfo = await _retrieveUserInfo();
if (userInfo == null) {
log.warning('Your credentials seems broken.\n'
- 'Run `pub logout` to delete your credentials and try again.');
+ 'Run `$topLevelProgram pub logout` to delete your credentials and try again.');
}
log.warning('You are already logged in as $userInfo\n'
'Run `pub logout` to log out and try again.');
@@ -47,8 +53,18 @@
if (userInfoRequest.statusCode != 200) return null;
try {
final userInfo = json.decode(userInfoRequest.body);
- return _UserInfo(userInfo['name'], userInfo['email']);
- } on FormatException {
+ final name = userInfo['name'];
+ final email = userInfo['email'];
+ if (name is String && email is String) {
+ return _UserInfo(name, email);
+ } else {
+ log.fine(
+ 'Bad response from $userInfoEndpoint: ${userInfoRequest.body}');
+ return null;
+ }
+ } on FormatException catch (e) {
+ log.fine(
+ 'Bad response from $userInfoEndpoint ($e): ${userInfoRequest.body}');
return null;
}
});