amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

Uploading using the S3 storage plugin gives a cryptic error message if file does not exist

Open JamesMcIntosh opened this issue 6 years ago • 7 comments

Describe the bug When uploading a file which doesn't exist using the S3 Storage Plugin an exception is thrown.

The error message gives no indication that the file does not exist on the device and is a very cumbersome to figure out what went wrong.

To Reproduce

final File file = File("this_file_does_not.exist");
final Map<String, String> metadata = <String, String>{};
metadata["name"] = "this_file_does_not.exist";

final S3UploadFileOptions options = S3UploadFileOptions(accessLevel: StorageAccessLevel.guest, metadata: metadata);
final UploadFileResult result = await Amplify.Storage.uploadFile(key: "this_file_does_not.exist", local: file, options: options);

Exception in the Dart stack

Unhandled Exception: PlatformException(AmplifyException, UPLOAD_FILE_OPERATION_FAILED, {PLATFORM_EXCEPTIONS: {
platform: Android, 
localizedErrorMessage: Issue uploading file., 
recoverySuggestion: See included exception for more details and suggestions to fix.
}})
AmplifyStorageS3MethodChannel.uploadFile (package:amplify_storage_s3/method_channel_storage_s3.dart:41:7)
<asynchronous suspension>
AmplifyStorageS3.uploadFile (package:amplify_storage_s3/amplify_storage_s3.dart:44:22)

I couldn't see any additional information or Exceptions in the stack trace on the Dart side.

With further digging I found the error originates on the Android side of the plugin in AmplifyStorageOperations.uploadFile(). This function throws away the cause of the exception and returns an uninformative wrapper AmplifyException

Amplify.Storage.uploadFile(
...
  { error ->
      if (!responseSent) {
          responseSent = true
          prepareError(flutterResult, FlutterStorageErrorMessage.UPLOAD_FILE_OPERATION_FAILED.name, error.localizedMessage, error.recoverySuggestion)
      } else {
          LOG.error(FlutterStorageErrorMessage.UPLOAD_FILE_OPERATION_FAILED.name, error)
      }
  }
)
...

Expected behavior

A useful error message is returned.

Platform Amplify Flutter current supports iOS and Android. This issue is reproducible in (check all that apply): [/] Android [?] iOS

JamesMcIntosh avatar Oct 07 '20 22:10 JamesMcIntosh

@Ashish-Nanda I looked a little bit more into it and the TransferUtility:564 is correctly throwing an IllegalArgumentException when the file does not exist, so the behaviour is consistent. I guess the main problem is that when an error is encountered all that happens is the prepareError message is being called with the following

prepareError(flutterResult, FlutterStorageErrorMessage.LIST_OPERATION_FAILED.name, error.localizedMessage, error.recoverySuggestion)

In these error cases the recoverySuggesting is See included exception for more details and suggestions to fix. which is rather useless as the exception is discarded, the Exception with all the details is actually error.cause

I've updated the original report.

JamesMcIntosh avatar Oct 08 '20 04:10 JamesMcIntosh

@JamesMcIntosh Thanks for your detailed report on the issue. I'm looking into this today. I'll make sure to add the error.cause for the exception raised in the Android platform code. However, I agree with your initial suggestion of failing fast in the dart layer itself by checking for the existence of the file using file.existsSync()

Will post an update once I make the change and validate the fix.

Ashish-Nanda avatar Oct 08 '20 17:10 Ashish-Nanda

Any update on this?

NimaSoroush avatar Mar 04 '21 20:03 NimaSoroush

@NimaSoroush Will be looking into this today and will update you once I have a PR up.

Ashish-Nanda avatar Mar 05 '21 12:03 Ashish-Nanda

@Ashish-Nanda : That would be great. Thanks

NimaSoroush avatar Mar 05 '21 12:03 NimaSoroush

Hi @NimaSoroush @JamesMcIntosh

You will get the error below if file does not exist


flutter: UploadFile Err: StorageException(message: There is a client side validation error for the field [local] with message: The file located at the `local` URL is missing, recoverySuggestion: Make sure the file exists before uploading to storage., underlyingException: null)

Feel free to test using the latest build and let us know if you got any issues

Thanks Mo

mo-malaka avatar Jun 21 '21 21:06 mo-malaka

@offlineprogrammer Thanks for letting me know, the error message is a lot more useful in explaining the situation.

At the moment I'm stuck back a few releases due to the version bump of the UUID dependency conflicting with other libraries and unable to test it.

JamesMcIntosh avatar Jun 27 '21 11:06 JamesMcIntosh