Search in sources :

Example 21 with AmplifyException

use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.

the class TestPredictionsCategory method create.

/**
 * Creates an instance of {@link PredictionsCategory} using the provided configuration resource.
 * @param context Android Context
 * @param resourceId Android resource ID for a configuration file
 * @return A PredictionsCategory instance using the provided configuration
 */
static PredictionsCategory create(@NonNull Context context, @RawRes int resourceId) {
    Objects.requireNonNull(context);
    final PredictionsCategory predictionsCategory = new PredictionsCategory();
    try {
        predictionsCategory.addPlugin(new AWSPredictionsPlugin(AWSMobileClient.getInstance()));
        CategoryConfiguration predictionsConfiguration = AmplifyConfiguration.fromConfigFile(context, resourceId).forCategoryType(CategoryType.PREDICTIONS);
        predictionsCategory.configure(predictionsConfiguration, context);
        predictionsCategory.initialize(context);
    } catch (AmplifyException initializationFailure) {
        throw new RuntimeException(initializationFailure);
    }
    return predictionsCategory;
}
Also used : AmplifyException(com.amplifyframework.AmplifyException) PredictionsCategory(com.amplifyframework.predictions.PredictionsCategory) CategoryConfiguration(com.amplifyframework.core.category.CategoryConfiguration)

Example 22 with AmplifyException

use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.

the class DevMenuEnvironmentFragment method getEnvironmentInfo.

/**
 * Returns the environment information to be displayed.
 * @return a SpannableStringBuilder containing the formatted environment information
 */
private SpannableStringBuilder getEnvironmentInfo() {
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
    EnvironmentInfo envInfo = new EnvironmentInfo();
    // Append the amplify plugin versions to stringBuilder
    stringBuilder.append(setBold("Amplify Plugins Information"));
    String pluginVersions = "\n" + envInfo.getPluginVersions() + "\n";
    stringBuilder.append(pluginVersions);
    // Append the developer environment information to stringBuilder
    stringBuilder.append(setBold("Developer Environment Information"));
    String devEnvInfo = "";
    try {
        devEnvInfo = envInfo.getDeveloperEnvironmentInfo(requireContext());
    } catch (AmplifyException error) {
        LOG.warn("Error reading developer environment information.");
    }
    if (devEnvInfo.isEmpty()) {
        stringBuilder.append("\nUnable to retrieve developer environment information.");
    } else {
        devEnvInfo = "\n" + devEnvInfo;
        stringBuilder.append(devEnvInfo);
    }
    return stringBuilder;
}
Also used : AmplifyException(com.amplifyframework.AmplifyException) SpannableString(android.text.SpannableString) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 23 with AmplifyException

use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.

the class EnvironmentInfo method getDeveloperEnvironmentInfo.

/**
 * Returns a String representation of information about the developer's environment.
 * @param context an Android Context
 * @return developer environment information
 * @throws AmplifyException if the developer environment information could not be read
 */
public String getDeveloperEnvironmentInfo(@NonNull Context context) throws AmplifyException {
    Context appContext = Objects.requireNonNull(context).getApplicationContext();
    final JSONObject envInfo;
    try {
        envInfo = Resources.readJsonResource(appContext, DEV_ENV_INFO_FILE_NAME);
    } catch (Resources.ResourceLoadingException resourceLoadingException) {
        throw new AmplifyException("Failed to find " + DEV_ENV_INFO_FILE_NAME + ".", resourceLoadingException, "Please ensure it is present in your project.");
    }
    StringBuilder formattedEnvInfo = new StringBuilder();
    for (String envItem : devEnvironmentItems.keySet()) {
        if (envInfo.has(envItem)) {
            String envValue;
            try {
                envValue = envInfo.getString(envItem);
            } catch (JSONException jsonError) {
                throw new AmplifyException("Error reading the developer environment information.", jsonError, "Check that " + DEV_ENV_INFO_FILE_NAME + ".json is properly formatted");
            }
            String devEnvInfoItem = devEnvironmentItems.get(envItem) + ": " + envValue + "\n";
            formattedEnvInfo.append(devEnvInfoItem);
        }
    }
    return formattedEnvInfo.toString();
}
Also used : Context(android.content.Context) AmplifyException(com.amplifyframework.AmplifyException) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Resources(com.amplifyframework.core.Resources)

Example 24 with AmplifyException

use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.

the class Category method configure.

/**
 * Configure category with provided AmplifyConfiguration object.
 * @param configuration Configuration for all plugins in the category
 * @param context An Android Context
 * @throws AmplifyException if already configured
 */
public final synchronized void configure(@NonNull CategoryConfiguration configuration, @NonNull Context context) throws AmplifyException {
    synchronized (state) {
        if (!State.NOT_CONFIGURED.equals(state.get())) {
            throw new AmplifyException("Category " + getCategoryType() + " has already been configured or is currently configuring.", "Ensure that you are only attempting configuration once.");
        }
        state.set(State.CONFIGURING);
        try {
            for (P plugin : getPlugins()) {
                String pluginKey = plugin.getPluginKey();
                JSONObject pluginConfig = configuration.getPluginConfig(pluginKey);
                plugin.configure(pluginConfig != null ? pluginConfig : new JSONObject(), context);
            }
            state.set(State.CONFIGURED);
        } catch (Throwable anyError) {
            state.set(State.CONFIGURATION_FAILED);
            throw anyError;
        }
    }
}
Also used : AmplifyException(com.amplifyframework.AmplifyException) JSONObject(org.json.JSONObject)

Example 25 with AmplifyException

use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.

the class ModelSchema method fromModelClass.

/**
 * Construct the ModelSchema from the {@link Model} class.
 *
 * @param clazz the instance of a model class
 * @return the ModelSchema object.
 * @throws AmplifyException If the conversion fails
 */
@NonNull
public static ModelSchema fromModelClass(@NonNull Class<? extends Model> clazz) throws AmplifyException {
    try {
        final List<Field> classFields = FieldFinder.findModelFieldsIn(clazz);
        final TreeMap<String, ModelField> fields = new TreeMap<>();
        final TreeMap<String, ModelAssociation> associations = new TreeMap<>();
        final TreeMap<String, ModelIndex> indexes = new TreeMap<>();
        final List<AuthRule> authRules = new ArrayList<>();
        // Set the model name and plural name (null if not provided)
        ModelConfig modelConfig = clazz.getAnnotation(ModelConfig.class);
        final String modelName = clazz.getSimpleName();
        final String modelPluralName = modelConfig != null && !modelConfig.pluralName().isEmpty() ? modelConfig.pluralName() : null;
        final String listPluralName = modelConfig != null && !modelConfig.listPluralName().isEmpty() ? modelConfig.listPluralName() : null;
        final String syncPluralName = modelConfig != null && !modelConfig.syncPluralName().isEmpty() ? modelConfig.syncPluralName() : null;
        if (modelConfig != null) {
            for (com.amplifyframework.core.model.annotations.AuthRule ruleAnnotation : modelConfig.authRules()) {
                authRules.add(new AuthRule(ruleAnnotation));
            }
        }
        for (Annotation annotation : clazz.getAnnotations()) {
            if (annotation.annotationType().isAssignableFrom(Indexes.class)) {
                Indexes indexesAnnotation = (Indexes) annotation;
                for (Index indexAnnotation : indexesAnnotation.value()) {
                    ModelIndex modelIndex = createModelIndex(indexAnnotation);
                    indexes.put(modelIndex.getIndexName(), modelIndex);
                }
            } else if (annotation.annotationType().isAssignableFrom(Index.class)) {
                ModelIndex modelIndex = createModelIndex((Index) annotation);
                indexes.put(modelIndex.getIndexName(), modelIndex);
            }
        }
        for (Field field : classFields) {
            final ModelField modelField = createModelField(field);
            if (modelField != null) {
                fields.put(field.getName(), modelField);
            }
            final ModelAssociation modelAssociation = createModelAssociation(field);
            if (modelAssociation != null) {
                associations.put(field.getName(), modelAssociation);
            }
        }
        return ModelSchema.builder().name(modelName).pluralName(modelPluralName).listPluralName(listPluralName).syncPluralName(syncPluralName).authRules(authRules).fields(fields).associations(associations).indexes(indexes).modelClass(clazz).build();
    } catch (Exception exception) {
        throw new AmplifyException("Error in constructing a ModelSchema.", exception, AmplifyException.TODO_RECOVERY_SUGGESTION);
    }
}
Also used : AmplifyException(com.amplifyframework.AmplifyException) ArrayList(java.util.ArrayList) Index(com.amplifyframework.core.model.annotations.Index) TreeMap(java.util.TreeMap) Indexes(com.amplifyframework.core.model.annotations.Indexes) Annotation(java.lang.annotation.Annotation) AmplifyException(com.amplifyframework.AmplifyException) Field(java.lang.reflect.Field) ModelConfig(com.amplifyframework.core.model.annotations.ModelConfig) NonNull(androidx.annotation.NonNull)

Aggregations

AmplifyException (com.amplifyframework.AmplifyException)34 DataStoreException (com.amplifyframework.datastore.DataStoreException)14 Model (com.amplifyframework.core.model.Model)10 ModelSchema (com.amplifyframework.core.model.ModelSchema)10 SerializedModel (com.amplifyframework.core.model.SerializedModel)10 HashMap (java.util.HashMap)9 Test (org.junit.Test)9 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)8 Collections (java.util.Collections)7 TimeUnit (java.util.concurrent.TimeUnit)7 Assert.assertEquals (org.junit.Assert.assertEquals)7 Mockito.mock (org.mockito.Mockito.mock)7 DataStoreConfiguration (com.amplifyframework.datastore.DataStoreConfiguration)6 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)6 JSONException (org.json.JSONException)6 StorageItemChange (com.amplifyframework.datastore.storage.StorageItemChange)5 Post (com.amplifyframework.testmodels.commentsblog.Post)5 ArrayList (java.util.ArrayList)5 JSONObject (org.json.JSONObject)5 Context (android.content.Context)4