Skip to content

Commit 39cc362

Browse files
committed
fix: catch exception in exists
1 parent b0795f3 commit 39cc362

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

packages/storage_client/lib/src/fetch.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ class Fetch {
3030
StackTrace stack,
3131
FetchOptions? options,
3232
) {
33-
if (error is http.Response && !(options?.noResolveJson == true)) {
33+
if (error is http.Response) {
34+
if (options?.noResolveJson == true) {
35+
return StorageException(
36+
error.body.isEmpty ? error.reasonPhrase ?? '' : error.body,
37+
statusCode: '${error.statusCode}',
38+
);
39+
}
3440
try {
3541
final data = json.decode(error.body) as Map<String, dynamic>;
3642
return StorageException.fromJson(data, '${error.statusCode}');

packages/storage_client/lib/src/storage_file_api.dart

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,18 @@ class StorageFileApi {
413413
Future<bool> exists(String path) async {
414414
final finalPath = _getFinalPath(path);
415415
final options = FetchOptions(headers: headers);
416-
final response = await _storageFetch.head(
417-
'$url/object/$finalPath',
418-
options: options,
419-
);
420-
return true;
416+
try {
417+
await _storageFetch.head(
418+
'$url/object/$finalPath',
419+
options: options,
420+
);
421+
return true;
422+
} on StorageException catch (e) {
423+
if (e.statusCode == '400' || e.statusCode == '404') {
424+
return false;
425+
}
426+
rethrow;
427+
}
421428
}
422429

423430
/// Retrieve URLs for assets in public buckets
@@ -481,7 +488,6 @@ class StorageFileApi {
481488
body,
482489
options: options,
483490
);
484-
print(response[0]);
485491
final fileObjects = List<FileObject>.from(
486492
(response as List).map(
487493
(item) => FileObject.fromJson(item),

packages/storage_client/test/client_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ void main() {
410410
});
411411

412412
test('check if object exists', () async {
413-
await storage.from(newBucketName).upload(uploadPath, file);
413+
await storage.from(newBucketName).upload('$uploadPath-exists', file);
414414
final res = await storage.from(newBucketName).exists(uploadPath);
415415
expect(res, true);
416416

0 commit comments

Comments
 (0)