Skip to content

Commit

Permalink
Merge pull request #1814 from ardriveapp/PE-6263-cease-all-uploads-af…
Browse files Browse the repository at this point in the history
…ter-getting-any-underfunded-response-from-turbo-d-2-n

PE-6263: feat(upload)
  • Loading branch information
thiagocarvalhodev authored Aug 19, 2024
2 parents e269382 + f94c8a5 commit 2ef4d2c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
12 changes: 12 additions & 0 deletions packages/ardrive_uploader/lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ class UnknownNetworkException implements NetworkException {
Object? error;
}

class UnderFundException implements ArDriveUploaderExceptions {
UnderFundException({
required this.message,
this.error,
});

@override
final String message;
@override
Object? error;
}

class ThumbnailUploadException implements UploadStrategyException {
ThumbnailUploadException({
required this.message,
Expand Down
4 changes: 3 additions & 1 deletion packages/ardrive_uploader/lib/src/turbo_upload_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class TurboUploadService {
}, onError: (error) {
onSendProgressTimer?.cancel();
_cancelTokens.remove(cancelToken);

throw error;
});
} catch (e) {
Expand Down Expand Up @@ -212,7 +213,8 @@ class TurboUploadService {
logger.i('[$dataItemId] DataItem confirmed!');
return response;
case 'UNDERFUNDED':
throw UploadCanceledException('Upload canceled. Underfunded.');
throw UnderFundException(
message: 'Upload canceled. Underfunded.', error: response.data);
case 'ASSEMBLING':
case 'VALIDATING':
case 'FINALIZING':
Expand Down
48 changes: 42 additions & 6 deletions packages/ardrive_uploader/lib/src/upload_controller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:ardrive_uploader/src/exceptions.dart';
import 'package:ardrive_uploader/src/upload_dispatcher.dart';
import 'package:ardrive_uploader/src/utils/logger.dart';
import 'package:arweave/arweave.dart';
Expand Down Expand Up @@ -181,7 +182,7 @@ class _UploadController implements UploadController {
/// the `_uploadDispatcher` should handle all the errors and return an `UploadResult`
logger.d('Error on UploadWorker. Task: ${e.toString()}');

_handleError(task: tasks[e.id]!, error: e);
_handleError(task: tasks[e.id]!, exception: e);
},
upload: (task) async {
final uploadResult = await _uploadDispatcher.send(
Expand All @@ -192,7 +193,7 @@ class _UploadController implements UploadController {
);

if (!uploadResult.success) {
_handleError(task: task, error: uploadResult.error);
_handleError(task: task, exception: uploadResult.error);
}
},
);
Expand Down Expand Up @@ -226,7 +227,7 @@ class _UploadController implements UploadController {
/// the `_uploadDispatcher` should handle all the errors and return an `UploadResult`
logger.d('Error on UploadWorker. Task: ${task.toString()}');

_handleError(task: task, error: e);
_handleError(task: task, exception: e);
},
upload: (task) async {
final uploadResult = await _uploadDispatcher.send(
Expand All @@ -237,7 +238,7 @@ class _UploadController implements UploadController {
);

if (!uploadResult.success) {
_handleError(task: task, error: uploadResult.error);
_handleError(task: task, exception: uploadResult.error);
}
},
maxTasks: 1,
Expand Down Expand Up @@ -361,6 +362,10 @@ class _UploadController implements UploadController {

@override
Future<void> cancel() async {
if (_isCanceled) {
return;
}

workerPool?.cancel();
_isCanceled = true;

Expand Down Expand Up @@ -398,13 +403,44 @@ class _UploadController implements UploadController {
/// Calls the callback to the caller that the task has failed
void _handleError({
required UploadTask task,
required Object? error,
required Object? exception,
}) {
if (_isCanceled) return;

final updatedTask =
tasks[task.id]!.copyWith(error: error, status: UploadStatus.failed);
tasks[task.id]!.copyWith(error: exception, status: UploadStatus.failed);

updateProgress(task: updatedTask);

if (exception is UploadStrategyException &&
exception.error is UnderFundException) {
_isCanceled = true;

final cancelableTask = tasks.values
.where((e) =>
e.status != UploadStatus.complete &&
e.status != UploadStatus.failed)
.toList();

final cancelTasksFutureAsFailedUploads = cancelableTask.map(
(task) async {
await task.cancelToken?.cancel();

task = task.copyWith(status: UploadStatus.failed);

_failedTasks.putIfAbsent(task.id, () => task);

updateProgress(task: task);
},
);

Future.wait(cancelTasksFutureAsFailedUploads).then((_) {
_onError(_failedTasks.values.toList());
});

return;
}

/// Callback to the caller that the task has failed
_onFailedTask(updatedTask);
}
Expand Down

0 comments on commit 2ef4d2c

Please sign in to comment.