Skip to content

Commit

Permalink
Merge pull request #1799 from ardriveapp/PE-6469-refactor-on-error-me…
Browse files Browse the repository at this point in the history
…thod-at-upload-cubit-to-avoid-stack-overflow-error-stack-overflow

PE-6469: refactor error handling to avoid calling onError and addError
  • Loading branch information
thiagocarvalhodev authored Jul 23, 2024
2 parents e42f207 + 8c01cff commit 75777c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
37 changes: 6 additions & 31 deletions lib/blocs/upload/upload_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class UploadCubit extends Cubit<UploadState> {
);
} catch (error, stacktrace) {
logger.e('error mounting the upload', error, stacktrace);
addError(error);
_emitError(error);
}
}

Expand Down Expand Up @@ -645,14 +645,6 @@ class UploadCubit extends Cubit<UploadState> {

uploadController.onDone(
(tasks) async {
logger.d('Upload folders and files finished... Verifying results');

if (tasks.any((element) => element.status == UploadStatus.failed)) {
logger.e('One or more tasks failed. Emitting error');
// if any of the files failed, we should throw an error
addError(Exception('Error uploading'));
}

emit(UploadComplete());

unawaited(_profileCubit.refreshBalance());
Expand Down Expand Up @@ -766,30 +758,15 @@ class UploadCubit extends Cubit<UploadState> {

uploadController.onDone(
(tasks) async {
logger.d('Upload files finished... Verifying results');

bool uploadSucced = true;

if (tasks.any((element) => element.status == UploadStatus.failed)) {
logger.e('One or more tasks failed. Emitting error');
// if any of the files failed, we should throw an error
addError(Exception('Error uploading'));

PlausibleEventTracker.trackUploadFailure();
uploadSucced = false;
}

unawaited(_profileCubit.refreshBalance());

// all files are uploaded

logger.i('Upload finished with success');
logger.i(
'Upload finished with success. Number of tasks: ${tasks.length}',
);

emit(UploadComplete());

if (uploadSucced) {
PlausibleEventTracker.trackUploadSuccess();
}
PlausibleEventTracker.trackUploadSuccess();
},
);

Expand Down Expand Up @@ -976,16 +953,14 @@ class UploadCubit extends Cubit<UploadState> {
emit(UploadFailure(error: UploadErrors.unknown));
}

@override
void onError(Object error, StackTrace stackTrace) {
void _emitError(Object error) {
if (error is TurboUploadTimeoutException) {
emit(UploadFailure(error: UploadErrors.turboTimeout));

return;
}

emit(UploadFailure(error: UploadErrors.unknown));
super.onError(error, stackTrace);
}

Future<void> cancelUpload() async {
Expand Down
15 changes: 13 additions & 2 deletions lib/services/arweave/arweave_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:convert';

import 'package:ardrive/core/crypto/crypto.dart';
import 'package:ardrive/entities/entities.dart';
import 'package:ardrive/services/arweave/arweave_service_exception.dart';
import 'package:ardrive/services/arweave/error/gateway_error.dart';
import 'package:ardrive/services/arweave/get_segmented_transaction_from_drive_strategy.dart';
import 'package:ardrive/services/services.dart';
Expand Down Expand Up @@ -117,8 +118,18 @@ class ArweaveService {

/// Returns the pending transaction fees of the specified address that is not reflected by `getWalletBalance()`.
Future<BigInt> getPendingTxFees(String address) async {
final query = await graphQLRetry.execute(PendingTxFeesQuery(
variables: PendingTxFeesArguments(walletAddress: address)));
final query = await graphQLRetry.execute(
PendingTxFeesQuery(
variables: PendingTxFeesArguments(
walletAddress: address,
),
),
);

if (query.data == null) {
throw ArweaveServiceException(
'Error fetching pending transaction fees. The query `PendingTxFeesQuery` returned null');
}

return query.data!.transactions.edges
.map((edge) => edge.node)
Expand Down
10 changes: 10 additions & 0 deletions lib/services/arweave/arweave_service_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ArweaveServiceException implements Exception {
final String message;

ArweaveServiceException(this.message);

@override
String toString() {
return 'ArweaveServiceException: $message';
}
}

0 comments on commit 75777c2

Please sign in to comment.