From b6a19b44237f44541b84fedb1de118c9e16d7883 Mon Sep 17 00:00:00 2001 From: El-Hassan Wanas Date: Mon, 26 Sep 2022 17:56:38 +0300 Subject: [PATCH] Fix error handling for code generation and update client --- dart_wormhole_william | 2 +- lib/views/shared/receive.dart | 1 - lib/views/shared/send.dart | 105 +++++++++++++++++----------------- 3 files changed, 53 insertions(+), 55 deletions(-) diff --git a/dart_wormhole_william b/dart_wormhole_william index d9debed1..0dadfe87 160000 --- a/dart_wormhole_william +++ b/dart_wormhole_william @@ -1 +1 @@ -Subproject commit d9debed1e9b77618b86d533ac46f854fb352cd27 +Subproject commit 0dadfe873291b9c73d967cd21461e745ee4f82ec diff --git a/lib/views/shared/receive.dart b/lib/views/shared/receive.dart index dbd3e272..524f7d38 100644 --- a/lib/views/shared/receive.dart +++ b/lib/views/shared/receive.dart @@ -136,7 +136,6 @@ class ReceiveSharedState extends ChangeNotifier { this.error = ''; this.errorMessage = error.toString(); this.errorTitle = SOMETHING_WENT_WRONG; - print("$ERROR_RECEIVING_FILE\n$error"); if (error is ClientError) { switch (error.errorCode) { diff --git a/lib/views/shared/send.dart b/lib/views/shared/send.dart index 04497b7b..10dfcd4c 100644 --- a/lib/views/shared/send.dart +++ b/lib/views/shared/send.dart @@ -97,15 +97,62 @@ class SendSharedState extends ChangeNotifier { }); } + void defaultErrorHandler(Object error) { + this.setState(() { + currentState = SendScreenStates.SendError; + this.errorMessage = "$ERROR_SENDING_FILE: $error"; + this.error = ''; + this.errorTitle = ERROR_SENDING_FILE; + + if (error is ClientError) { + switch (error.errorCode) { + case ErrCodeTransferRejected: + this.errorTitle = TRANSFER_CANCELLED; + this.error = THE_RECEIVER_REJECTED_THIS_TRANSFER; + break; + case ErrCodeTransferCancelled: + this.errorTitle = TRANSFER_CANCELLED; + this.error = YOU_HAVE_CANCELLED_THE_TRANSFER; + break; + case ErrCodeTransferCancelledByReceiver: + this.errorTitle = TRANSFER_CANCELLED_INTERRUPTED; + this.error = EITHER_THE_TRANSFER_WAS_CANCELLED_BY; + break; + case ErrCodeWrongCode: + this.errorTitle = OOPS; + this.error = THE_RECEIVER_HAS_ENTERED_THE_WRONG_CODE; + break; + case ErrCodeSendTextError: + this.errorTitle = SOMETHING_WENT_WRONG; + // TODO: map error to user friendly name (case invalid nameplate) + break; + case ErrCodeSendFileError: + this.errorTitle = SOMETHING_WENT_WRONG; + // TODO: map error to user friendly name (case invalid nameplate) + break; + case ErrCodeConnectionRefused: + this.errorTitle = OOPS; + this.error = ERR_CONNECTION_REFUSED; + break; + default: + this.errorTitle = SOMETHING_WENT_WRONG; + // TODO: map error to user friendly name (case invalid nameplate) + break; + } + } + }); + + throw error; + } + Future send(f.File file) async { setState(() { sendingFile = file; currentState = SendScreenStates.CodeGenerating; }); - return await client - .sendFile(file, progress.progressHandler) - .then((result) async { + return await client.sendFile(file, progress.progressHandler).then( + (result) async { setState(() { code = result.code; currentState = SendScreenStates.CodeGenerated; @@ -116,55 +163,8 @@ class SendSharedState extends ChangeNotifier { setState(() { currentState = SendScreenStates.FileSent; }); - }, onError: (error, stacktrace) { - this.setState(() { - currentState = SendScreenStates.SendError; - this.errorMessage = "$ERROR_SENDING_FILE: $error"; - this.errorTitle = ERROR_SENDING_FILE; - - print("$ERROR_SENDING_FILE\n$error\n$stacktrace"); - - if (error is ClientError) { - switch (error.errorCode) { - case ErrCodeTransferRejected: - this.errorTitle = TRANSFER_CANCELLED; - this.error = THE_RECEIVER_REJECTED_THIS_TRANSFER; - break; - case ErrCodeTransferCancelled: - this.errorTitle = TRANSFER_CANCELLED; - this.error = YOU_HAVE_CANCELLED_THE_TRANSFER; - break; - case ErrCodeTransferCancelledByReceiver: - this.errorTitle = TRANSFER_CANCELLED_INTERRUPTED; - this.error = EITHER_THE_TRANSFER_WAS_CANCELLED_BY; - break; - case ErrCodeWrongCode: - this.errorTitle = OOPS; - this.error = THE_RECEIVER_HAS_ENTERED_THE_WRONG_CODE; - break; - case ErrCodeSendTextError: - this.errorTitle = SOMETHING_WENT_WRONG; - // TODO: map error to user friendly name (case invalid nameplate) - break; - case ErrCodeSendFileError: - this.errorTitle = SOMETHING_WENT_WRONG; - // TODO: map error to user friendly name (case invalid nameplate) - break; - case ErrCodeConnectionRefused: - this.errorTitle = OOPS; - this.error = ERR_CONNECTION_REFUSED; - break; - default: - this.errorTitle = SOMETHING_WENT_WRONG; - // TODO: map error to user friendly name (case invalid nameplate) - break; - } - } - }); - - throw error; - }); - }); + }, onError: defaultErrorHandler); + }, onError: defaultErrorHandler); } Widget widgetByState( @@ -197,7 +197,6 @@ class SendSharedState extends ChangeNotifier { selectingFile = true; }); await getFilePicker().showSelectFile().onError((error, stackTrace) { - print(error); throw error!; }).then((file) async { await send(file);