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.31k stars 243 forks source link

Datastore error handler has unparsable error message #4869

Open amritnew opened 4 months ago

amritnew commented 4 months ago

Description

While testing my Flutter app on the iOS simulator, I came across an issue. It occurred during the execution of Datastore.save, where the mutation failed due to a client error. Below is the Datastore initialization function. In this function, errorHandler is expected to return a message that can be parsed to obtain the error message.

AmplifyDataStore({
    required ModelProviderInterface modelProvider,
    Function(AmplifyException)? errorHandler,
    DataStoreConflictHandler? conflictHandler,
    List<DataStoreSyncExpression> syncExpressions = const [],
    int? syncInterval,
    int? syncMaxRecords,
    int? syncPageSize,
    AuthModeStrategy authModeStrategy = AuthModeStrategy.defaultStrategy,
  })

Below is the message I am getting inside errorHandler

AmplifyDataStore(
        modelProvider: ModelProvider.instance,
        // Error handler for any error occurs during any datastore expression
        errorHandler: (error) {
          Log('Amplify Config', 'AWS Error: $error');
        },
        syncExpressions: syncExpression);
Message: GraphQL service returned a successful response containing errors: [Amplify.GraphQLError(message: "The conditional request failed (Service: DynamoDb, Status Code: 400, Request ID: 7083O037M1FTFRK038A4CI9H43VV4KQNSO5AEMVJF66Q9ASUAAJG)", locations: Optional([Amplify.GraphQLError.Location(line: 2, column: 3)]), path: Optional([Amplify.JSONValue.string("createTodo")]), extensions: Optional(["errorType": Amplify.JSONValue.string("Code"), "data": Amplify.JSONValue.null, "errorInfo": Amplify.JSONValue.null]))]

Recovery suggestion: The list of `GraphQLError` contains service-specific messages

Underline exception: The operation couldn’t be completed. (Amplify.GraphQLResponseError<AWSPluginsCore.MutationSync<AWSPluginsCore.AnyModel>> error 0.)

As I deep drive into the amplify_datastore iOS SDK, I noticed that the createSerializedError function below sends an error description, which complicates the process of parsing the error.

createSerializedError(error: AmplifyError) -> [String: String] {
        return createSerializedError(message: error.errorDescription,
                                     recoverySuggestion: error.recoverySuggestion,
                                     underlyingError: error.underlyingError?.localizedDescription)
    }

Error passed to flutter apps should be in either a map or a json, so that flutter client can parse the error.

Categories

Steps to Reproduce

  1. Create models as in schema
  2. Initialize Datastore
  3. Do Datastore.save()
  4. Fails all incoming request in appsync or intercept iOS simulator request using any interceptor tool like Charles proxy OR Proxyman.
  5. Print error object in error handler

Screenshots

No response

Platforms

Flutter Version

3.19.0

Amplify Flutter Version

1.7.0

Deployment Method

Amplify CLI

Schema

type Todo @model {
    id: String!
    message: String!
    emails: [String!]!
    expirationUnixTime: AWSTimestamp
}
khatruong2009 commented 4 months ago

Hi @amritnew, we are taking a look at this now and will get back to you with any updates.