Skip to content

Commit 44b5c5e

Browse files
committed
refactor(login): improve error handling and logging in user info retrieval
1 parent d2d656e commit 44b5c5e

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

lib/src/command/login.dart

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,33 @@ class LoginCommand extends PubCommand {
5353
return await oauth2.withClient((client) async {
5454
final discovery = await oauth2.fetchOidcDiscoveryDocument();
5555
final userInfoEndpoint = discovery['userinfo_endpoint'];
56+
5657
if (userInfoEndpoint is! String) {
57-
log.fine('Bad discovery document. userinfo_endpoint not a String');
58+
log.fine(
59+
'Invalid discovery document: userinfo_endpoint is not a String',
60+
);
5861
return null;
5962
}
60-
final userInfoRequest = await client.get(Uri.parse(userInfoEndpoint));
61-
if (userInfoRequest.statusCode != 200) return null;
63+
64+
final response = await client.get(Uri.parse(userInfoEndpoint));
65+
if (response.statusCode != 200) {
66+
log.fine('Failed to fetch user info: HTTP ${response.statusCode}');
67+
return null;
68+
}
69+
6270
try {
63-
switch (json.decode(userInfoRequest.body)) {
64-
case {'name': final String? name, 'email': final String email}:
65-
return _UserInfo(name, email);
66-
default:
67-
log.fine(
68-
'Bad response from $userInfoEndpoint: ${userInfoRequest.body}',
69-
);
70-
return null;
71+
final decoded = json.decode(response.body);
72+
if (decoded case {
73+
'name': final String? name,
74+
'email': final String email,
75+
}) {
76+
return _UserInfo(name, email);
77+
} else {
78+
log.fine('Unexpected user info format: ${response.body}');
79+
return null;
7180
}
7281
} on FormatException catch (e) {
73-
log.fine(
74-
'Bad response from $userInfoEndpoint ($e): ${userInfoRequest.body}',
75-
);
82+
log.fine('Failed to decode user info: $e\nResponse: ${response.body}');
7683
return null;
7784
}
7885
});

0 commit comments

Comments
 (0)