Skip to content

Commit

Permalink
fix(shorebird_cli): cache artifact download error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Aug 10, 2023
1 parent 924e910 commit 4b1a5c7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/shorebird_cli/lib/src/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import 'package:shorebird_cli/src/platform.dart';
import 'package:shorebird_cli/src/process.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';

class CacheUpdateFailure implements Exception {
const CacheUpdateFailure(this.message);

final String message;

@override
String toString() => 'CacheUpdateFailure: $message';
}

typedef ArchiveExtracter = Future<void> Function(
String archivePath,
String outputPath,
Expand Down Expand Up @@ -128,7 +137,23 @@ abstract class CachedArtifact {

Future<void> update() async {
final request = http.Request('GET', Uri.parse(storageUrl));
final response = await cache.httpClient.send(request);
final http.StreamedResponse response;
try {
response = await cache.httpClient.send(request);
} catch (error) {
throw CacheUpdateFailure(
'''
Failed to download $name: $error
If you're behind a firewall/proxy, please, make sure shorebird_cli is
allowed to access $storageUrl.''',
);
}

if (response.statusCode != HttpStatus.ok) {
throw CacheUpdateFailure(
'''Failed to download $name: ${response.statusCode} ${response.reasonPhrase}''',
);
}

await extractArtifact(response.stream, location.path);

Expand Down
43 changes: 43 additions & 0 deletions packages/shorebird_cli/test/src/cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ void main() {
expect(Cache.new, returnsNormally);
});

group(CacheUpdateFailure, () {
test('overrides toString', () {
const exception = CacheUpdateFailure('test');
expect(exception.toString(), equals('CacheUpdateFailure: test'));
});
});

group('getArtifactDirectory', () {
test('returns correct directory', () {
final directory = runWithOverrides(
Expand Down Expand Up @@ -172,6 +179,42 @@ void main() {

group('updateAll', () {
group('patch', () {
test('throws CacheUpdateFailure if a SocketException is thrown',
() async {
const exception = SocketException('test');
when(() => httpClient.send(any())).thenThrow(exception);
await expectLater(
runWithOverrides(cache.updateAll),
throwsA(
isA<CacheUpdateFailure>().having(
(e) => e.message,
'message',
contains('Failed to download patch: $exception'),
),
),
);
});

test('throws CacheUpdateFailure if a non-200 is returned', () async {
when(() => httpClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(
const Stream.empty(),
HttpStatus.notFound,
reasonPhrase: 'Not Found',
),
);
await expectLater(
runWithOverrides(cache.updateAll),
throwsA(
isA<CacheUpdateFailure>().having(
(e) => e.message,
'message',
contains('Failed to download patch: 404 Not Found'),
),
),
);
});

test('downloads correct artifacts', () async {
final patchArtifactDirectory = runWithOverrides(
() => cache.getArtifactDirectory('patch'),
Expand Down

0 comments on commit 4b1a5c7

Please sign in to comment.