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;
}
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;
}
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();
}
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;
}
}
}
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);
}
}
Aggregations