aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.32k stars 248 forks source link

Amplify exceptions cannot be caught -- they show up as "platform exceptions" and the error thrown "Translating platform exception failed" cannot be caught in the client code #2238

Closed dorontal closed 2 years ago

dorontal commented 2 years ago

Description

Amplify exceptions cannot be caught -- they show up as "platform exceptions" and the error thrown "Translating platform exception failed" cannot be caught in the client code.

For example, here is the complaint I get in VSCode in red when an exception is not caught and the app crashes -- after trying to get a file from S3 which does not exist:

PlatformException (PlatformException(StorageException, Translating platform exception failed. Please take a look at
https://github.com/aws-amplify/amplify-flutter/issues to see if there are any existing issues that
match your scenario, and file an issue with the details of the bug if there isn't., {recoverySuggestion: See attached exception for more information and suggestions, underlyingException: com.amazonaws.services.s3.model.AmazonS3Exception: The specified key does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchKey; Request ID: MTP4K4J56D80ZP77), S3 Extended Request ID: T/TlvCPu1tABRBNeCdQg9INbXka/IXU8X0vRTef5itMbn8CEicmN7uAzu2BXjU+Ew5eVZMPxdDE=, message: Something went wrong with your AWS S3 Storage download file operation}, null))

All your exceptions are showing up as this PlatformException wrapped object that generates errors like the above when you try to catch an Amplify exception in the client app code.

Categories

Steps to Reproduce

Just run any skeleton app and cause an Amplify exception in your code. E.g. use this code

try {
  final AuthUser authUser = await Amplify.Auth.getCurrentUser();
} on AuthException catch (e) {
  dev.log('$e');
}

while no user is signed in to generate an amplify "user not signed in" exception - put a breakpoint in the catch block and the exception will not be caught. You will get the app crashing with a complaint very similar to what you see in this issue: https://github.com/aws-amplify/amplify-flutter/issues/385

Screenshots

No response

Platforms

Android Device/Emulator API Level

API 29, API 31

Environment

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.4, on Debian GNU/Linux 11 (bullseye)
    5.10.0-18-amd64, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version
    32.1.0-rc1)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2021.1)
[✓] VS Code (version 1.72.1)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

• No issues found!

Dependencies

Dart SDK 2.18.2
Flutter SDK 3.3.4
tracktunes_app_aws 0.0.1-alpha.4

dependencies:
- amplify_api 0.6.8 [amplify_api_android amplify_api_ios amplify_core amplify_flutter aws_common collection flutter meta plugin_platform_interface]
- amplify_auth_cognito 0.6.8 [amplify_auth_cognito_android amplify_auth_cognito_ios amplify_core aws_common collection flutter meta plugin_platform_interface]
- amplify_datastore 0.6.8 [flutter amplify_datastore_plugin_interface amplify_core plugin_platform_interface meta collection async]
- amplify_flutter 0.6.8 [amplify_core amplify_datastore_plugin_interface amplify_flutter_android amplify_flutter_ios aws_common collection flutter meta plugin_platform_interface]
- amplify_storage_s3 0.6.8 [amplify_storage_s3_android amplify_storage_s3_ios amplify_core aws_common flutter meta plugin_platform_interface path_provider path]
- cached_network_image 3.2.2 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web]
- cupertino_icons 1.0.5
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_hooks 0.18.5+1 [flutter]
- google_fonts 3.0.1 [flutter http path_provider crypto]
- hooks_riverpod 2.0.2 [collection flutter flutter_hooks flutter_riverpod riverpod state_notifier]
- image 3.2.0 [archive meta xml]
- image_picker 0.8.6 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_platform_interface]
- mime 1.0.2
- path 1.8.2
- path_provider 2.0.11 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows]
- rflutter_alert 2.0.4 [flutter]
- shared_preferences 2.0.15 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows]

transitive dependencies:
- amplify_api_android 0.6.8 [flutter]
- amplify_api_ios 0.6.8 [amplify_core flutter]
- amplify_auth_cognito_android 0.6.8 [flutter]
- amplify_auth_cognito_ios 0.6.8 [amplify_core flutter]
- amplify_core 0.6.8 [aws_common collection flutter intl json_annotation meta plugin_platform_interface uuid]
- amplify_datastore_plugin_interface 0.6.8 [amplify_core collection flutter meta]
- amplify_flutter_android 0.6.8 [flutter]
- amplify_flutter_ios 0.6.8 [amplify_core flutter]
- amplify_storage_s3_android 0.6.8 [flutter]
- amplify_storage_s3_ios 0.6.8 [flutter]
- archive 3.3.1 [crypto path]
- async 2.9.0 [collection meta]
- aws_common 0.1.1 [async collection http meta stream_transform uuid]
- cached_network_image_platform_interface 2.0.0 [flutter flutter_cache_manager]
- cached_network_image_web 1.0.2 [flutter flutter_cache_manager cached_network_image_platform_interface]
- characters 1.2.1
- clock 1.1.1
- collection 1.16.0
- cross_file 0.3.3+2 [js meta]
- crypto 3.0.2 [typed_data]
- ffi 2.0.1
- file 6.1.4 [meta path]
- flutter_blurhash 0.7.0 [flutter]
- flutter_cache_manager 3.3.0 [clock collection file flutter http path path_provider pedantic rxdart sqflite uuid]
- flutter_plugin_android_lifecycle 2.0.7 [flutter]
- flutter_riverpod 2.0.2 [collection flutter meta riverpod state_notifier]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- http 0.13.5 [async http_parser meta path]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- image_picker_android 0.8.5+3 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface]
- image_picker_for_web 2.1.10 [flutter flutter_web_plugins image_picker_platform_interface]
- image_picker_ios 0.8.6+1 [flutter image_picker_platform_interface]
- image_picker_platform_interface 2.6.2 [cross_file flutter http plugin_platform_interface]
- intl 0.17.0 [clock path]
- js 0.6.4
- json_annotation 4.7.0 [meta]
- material_color_utilities 0.1.5
- meta 1.8.0
- octo_image 1.0.2 [flutter flutter_blurhash]
- path_provider_android 2.0.20 [flutter path_provider_platform_interface]
- path_provider_ios 2.0.11 [flutter path_provider_platform_interface]
- path_provider_linux 2.1.7 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_macos 2.0.6 [flutter path_provider_platform_interface]
- path_provider_platform_interface 2.0.5 [flutter platform plugin_platform_interface]
- path_provider_windows 2.1.3 [ffi flutter path path_provider_platform_interface win32]
- pedantic 1.11.1
- petitparser 5.0.0 [meta]
- platform 3.1.0
- plugin_platform_interface 2.1.3 [meta]
- process 4.2.4 [file path platform]
- riverpod 2.0.2 [collection meta stack_trace state_notifier]
- rxdart 0.27.5
- shared_preferences_android 2.0.13 [flutter shared_preferences_platform_interface]
- shared_preferences_ios 2.1.1 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.1.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_macos 2.0.4 [flutter shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.1.0 [flutter plugin_platform_interface]
- shared_preferences_web 2.0.4 [flutter flutter_web_plugins shared_preferences_platform_interface]
- shared_preferences_windows 2.1.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- sky_engine 0.0.99
- source_span 1.9.0 [collection path term_glyph]
- sqflite 2.1.0+1 [flutter sqflite_common path]
- sqflite_common 2.3.0 [synchronized path meta]
- stack_trace 1.10.0 [path]
- state_notifier 0.7.2+1 [meta]
- stream_transform 2.0.1
- string_scanner 1.1.1 [source_span]
- synchronized 3.0.0+3
- term_glyph 1.2.1
- typed_data 1.3.1 [collection]
- uuid 3.0.6 [crypto]
- vector_math 2.1.2
- win32 3.0.0 [ffi]
- xdg_directories 0.2.0+2 [meta path process]
- xml 6.1.0 [collection meta petitparser]

Device

Android Emulator

OS

Android 8

Deployment Method

Amplify CLI

CLI Version

10.2.3

Additional Context

This happened as soon as I upgraded the CLI from version 10.2.1 to 10.2.3

Amplify Config

const amplifyconfig = ''' { "UserAgent": "aws-amplify-cli/2.0", "Version": "1.0", "api": { "plugins": { "awsAPIPlugin": { "tracktunes": { "endpointType": "GraphQL", "endpoint": "https://?????????????.appsync-api.us-east-1.amazonaws.com/graphql", "region": "us-east-1", "authorizationType": "AWS_IAM" }, "tracktunesCognito": { "endpointType": "GraphQL", "endpoint": "https://?????????????.appsync-api.us-east-1.amazonaws.com/graphql", "region": "us-east-1", "authorizationType": "AMAZON_COGNITO_USER_POOLS" }, "lambdaFunctions": { "endpointType": "REST", "endpoint": "https://?????????.execute-api.us-east-1.amazonaws.com/dev", "region": "us-east-1", "authorizationType": "AWS_IAM" }, "lambdaFunctionsCognito": { "endpointType": "REST", "endpoint": "https://?????????.execute-api.us-east-1.amazonaws.com/dev", "region": "us-east-1", "authorizationType": "AMAZON_COGNITO_USER_POOLS" } } } }, "auth": { "plugins": { "awsCognitoAuthPlugin": { "UserAgent": "aws-amplify-cli/0.1.0", "Version": "0.1.0", "IdentityManager": { "Default": {} }, "CredentialsProvider": { "CognitoIdentity": { "Default": { "PoolId": "us-east-1:??????????????????????", "Region": "us-east-1" } } }, "CognitoUserPool": { "Default": { "PoolId": "us-east-1_fQp7Vz93X", "AppClientId": "???????????????????", "Region": "us-east-1" } }, "Auth": { "Default": { "authenticationFlowType": "USER_SRP_AUTH", "socialProviders": [], "usernameAttributes": [ "EMAIL" ], "signupAttributes": [ "EMAIL" ], "passwordProtectionSettings": { "passwordPolicyMinLength": 8, "passwordPolicyCharacters": [] }, "mfaConfiguration": "OFF", "mfaTypes": [ "SMS" ], "verificationMechanisms": [ "EMAIL" ] } }, "AppSync": { "Default": { "ApiUrl": "https://?????????????.appsync-api.us-east-1.amazonaws.com/graphql", "Region": "us-east-1", "AuthMode": "AWS_IAM", "ClientDatabasePrefix": "tracktunes_AWS_IAM" }, "tracktunes_AMAZON_COGNITO_USER_POOLS": { "ApiUrl": "https://?????????????.appsync-api.us-east-1.amazonaws.com/graphql", "Region": "us-east-1", "AuthMode": "AMAZON_COGNITO_USER_POOLS", "ClientDatabasePrefix": "tracktunes_AMAZON_COGNITO_USER_POOLS" } }, "S3TransferUtility": { "Default": { "Bucket": "?????????????", "Region": "us-east-1" } } } } }, "storage": { "plugins": { "awsS3StoragePlugin": { "bucket": "?????????????", "region": "us-east-1", "defaultAccessLevel": "guest" } } } }''';

abdallahshaban557 commented 2 years ago

@dorontal - thank you for creating this ticket so promptly - we will check this out!

dorontal commented 2 years ago

Sounds great, thanks so much @abdallahshaban557 - FYI: this happened after a routine upgrade of the CLI and all SDK libraries, which I do about once per week. The upgrade went from version 10.2.1 to 10.2.3 when this happened.

abdallahshaban557 commented 2 years ago

understood - saw that comment from you on the previous issue and I have passed that info to our support team! We will get back to you!

ragingsquirrel3 commented 2 years ago

@dorontal I wasn't able to reproduce this issue even after upgrading CLI and setting up a new Amplify backend with the new CLI. After you upgraded the CLI, did you run any amplify commands like push/pull? If so, did you make any backend updates first? I'm trying to better understand how CLI update might be connected here and eventually reproduce.

Looking at https://github.com/aws-amplify/amplify-flutter/issues/385 am wondering if you tried running flutter clean and restarting vscode?

dorontal commented 2 years ago

@ragingsquirrel3 I did see #385 and did try the flutter clean followed by a VSCode restart but to no avail. You mean you used all new versions to create a new project and then to cause in it an Amplify exception and you were able to catch such exceptions with a try-catch? That's interesting. It does not work for me. Amplify exceptions such as StorageException or AuthException cannot be caught right now in my code and this started right after the upgrade without any changes to the code.

ragingsquirrel3 commented 2 years ago

Yes, I created a new amplify project after updating to newest CLI version, ran app in android and performed an operation that throws an exception. I was able to catch the exception as AuthException without issue so not sure how I can reproduce this. This is so far only on Android, correct?

dorontal commented 2 years ago

Yes, only tested this on the usual Android emulator and I don't have a way to test on iOS. Thanks very much for clarifying. I did try to re-install everything from scratch (amplify folder, cli, libraries) and restarted but got the same error. If this persists, I will create a small app that demonstrates it.

ragingsquirrel3 commented 2 years ago

@dorontal have you tried manually downgrading your Amplify CLI to the version you used before? Also, did you run amplify push/pull after updating it? CLI update by itself would be very strange cause unless it updated backend/config file with new version (which would still be strange). I don't understand how CLI update would cause this kind of error but as a sanity check might be good to try the old version so not to focus on CLI version if that's not really root cause.

dorontal commented 2 years ago

@ragingsquirrel3 I understand and also find it counterintuitive that a CLI upgrade would cause a bug in exception handling code. I have another machine with CLI version 10.0.0 installed on it. I copied the entire project over and it ran flawlessly on that machine as-is without a change. Several exceptions got called, e.g. when getCurrentUser() was called while nobody was logged in and these exceptions now get caught/handled just fine on the other machine. If I have time to look further into the differences between these two, I will, but probably would just prefer to continue working for now. At some point I'll run amplify upgrade on the other machine where things currently work with version 10.0.0 and will report if the upgrade breaks things, but it wasn't just a CLI upgrade after which this issue showed up, it was a CLI upgrade plus a rebuild (automatic, via expect scripts) of everything after an amplify delete.

tracktunes commented 2 years ago

@ragingsquirrel3 I never figured out why exception handling temporarily failed after the upgrade of Amplify libraries to version 0.6.8 and of the CLI from version 10.2.1 to 10.2.3 (it wasn't just a CLI upgrade before as I had indicated).

But after upgrading to CLI version 10.3.0 (also later to 10.3.1) and to the library versions 0.6.9and everything works as expected - exceptions can be caught. I believe this issue can be closed now. Thanks for your help. Please feel free to close this.

ragingsquirrel3 commented 2 years ago

Glad this issue went away and thanks for the info.