chore(shorebird_cli): improve error messaging for add collaborator 403s (#722)

This commit is contained in:
Bryan Oltman
2023-06-28 16:59:58 -04:00
committed by GitHub
parent 247e55bc7f
commit 2cf38345f9
4 changed files with 51 additions and 0 deletions
@@ -7,6 +7,7 @@ import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/shorebird_config_mixin.dart';
import 'package:shorebird_cli/src/shorebird_environment.dart';
import 'package:shorebird_cli/src/shorebird_validation_mixin.dart';
import 'package:shorebird_code_push_client/shorebird_code_push_client.dart';
/// {@template add_collaborators_command}
/// `shorebird collaborators add`
@@ -86,6 +87,12 @@ ${styleBold.wrap(lightGreen.wrap('🚀 Ready to add a new collaborator!'))}
try {
await client.createCollaborator(appId: appId, email: collaborator);
progress.complete();
} on CodePushForbiddenException {
progress.fail();
logger.err(
'You do not have permission to add collaborators to this app.',
);
return ExitCode.software.code;
} catch (error) {
progress.fail();
logger.err('$error');
@@ -104,6 +104,26 @@ void main() {
);
});
test(
'''exits with code 70 if user does not have permission to add collaborators''',
() async {
final error = CodePushForbiddenException(
message: 'oops something went wrong',
);
when(
() => codePushClient.createCollaborator(
appId: any(named: 'appId'),
email: any(named: 'email'),
),
).thenThrow(error);
expect(await runWithOverrides(command.run), ExitCode.software.code);
verify(
() => logger.err(
'You do not have permission to add collaborators to this app.',
),
).called(1);
});
test(
'returns ExitCode.software '
'when adding a collaborator fails', () async {
@@ -22,6 +22,14 @@ class CodePushException implements Exception {
String toString() => '$message${details != null ? '\n$details' : ''}';
}
/// {@template code_push_forbidden_exception}
/// Exception thrown when a 403 response is received.
/// {@endtemplate}
class CodePushForbiddenException extends CodePushException {
/// {@macro code_push_forbidden_exception}
CodePushForbiddenException({required super.message, super.details});
}
/// {@template code_push_conflict_exception}
/// Exception thrown when a 409 response is received.
/// {@endtemplate}
@@ -494,6 +502,7 @@ class CodePushClient {
HttpStatus.conflict => CodePushConflictException.new,
HttpStatus.notFound => CodePushNotFoundException.new,
HttpStatus.upgradeRequired => CodePushUpgradeRequiredException.new,
HttpStatus.forbidden => CodePushForbiddenException.new,
_ => CodePushException.new,
};
@@ -108,6 +108,21 @@ void main() {
);
});
test('throws a permission exception if the http response code is 403',
() async {
when(() => httpClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(
Stream.value(utf8.encode(json.encode(errorResponse.toJson()))),
HttpStatus.forbidden,
),
);
expect(
codePushClient.createCollaborator(appId: appId, email: email),
throwsA(isA<CodePushForbiddenException>()),
);
});
test('throws an exception if the http request fails', () async {
when(() => httpClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(