Skip to content

Commit 092ce2c

Browse files
chore: improve missing config error messages (#1038)
The Amplify documentation suggests to configure the library with this call: ```kotlin Amplify.configure(applicationContext) ``` When configured in this way, the library will attempt to load a file from the the user's application project. The file is expected to live at `app/src/main/res/raw/amplifyconfiguration.json`. If this file is *not* present, the previous error message was not very helpful. This commit updates the error message, to give the user a direct call to action. This PR also updates a few related resource loading messages, to make their error cases more clear.
1 parent 0e083e4 commit 092ce2c

File tree

3 files changed

+64
-33
lines changed

3 files changed

+64
-33
lines changed

core/src/main/java/com/amplifyframework/core/AmplifyConfiguration.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static AmplifyConfiguration fromJson(@NonNull JSONObject json) throws Amp
112112
@SuppressWarnings("WeakerAccess")
113113
@NonNull
114114
public static AmplifyConfiguration fromConfigFile(@NonNull Context context) throws AmplifyException {
115-
return builder(context, Resources.getRawResourceId(context, DEFAULT_IDENTIFIER)).build();
115+
return builder(context).build();
116116
}
117117

118118
/**
@@ -206,7 +206,16 @@ public CategoryConfiguration forCategoryType(@NonNull CategoryType categoryType)
206206
*/
207207
@NonNull
208208
public static Builder builder(@NonNull Context context) throws AmplifyException {
209-
return builder(context, Resources.getRawResourceId(context, DEFAULT_IDENTIFIER));
209+
try {
210+
@RawRes final int resourceId = Resources.getRawResourceId(context, DEFAULT_IDENTIFIER);
211+
return builder(context, resourceId);
212+
} catch (Resources.ResourceLoadingException resourceLoadingException) {
213+
throw new AmplifyException(
214+
"Failed to load the " + DEFAULT_IDENTIFIER + " configuration file.", resourceLoadingException,
215+
"Is there an Amplify configuration file present in your app project, under " +
216+
"./app/src/main/res/raw/" + DEFAULT_IDENTIFIER + "?"
217+
);
218+
}
210219
}
211220

212221
/**
@@ -219,11 +228,18 @@ public static Builder builder(@NonNull Context context) throws AmplifyException
219228
* @throws AmplifyException If there is a problem in the config file
220229
*/
221230
@NonNull
222-
public static Builder builder(
223-
@NonNull Context context,
224-
@RawRes int configFileResourceId
225-
) throws AmplifyException {
226-
return builder(Resources.readJsonResourceFromId(Objects.requireNonNull(context), configFileResourceId));
231+
public static Builder builder(@NonNull Context context, @RawRes int configFileResourceId)
232+
throws AmplifyException {
233+
Objects.requireNonNull(context);
234+
try {
235+
return builder(Resources.readJsonResourceFromId(context, configFileResourceId));
236+
} catch (Resources.ResourceLoadingException resourceLoadingException) {
237+
throw new AmplifyException(
238+
"Failed to read JSON from resource = " + configFileResourceId, resourceLoadingException,
239+
"If you are attempting to load a custom configuration file, please ensure that it exists " +
240+
"in your application project under app/src/main/res/raw/<YOUR_CUSTOM_CONFIG_FILE>."
241+
);
242+
}
227243
}
228244

229245
/**
@@ -298,4 +314,3 @@ public AmplifyConfiguration build() {
298314
}
299315
}
300316
}
301-

core/src/main/java/com/amplifyframework/core/Resources.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import android.content.res.Resources.NotFoundException;
2020
import androidx.annotation.RawRes;
2121

22-
import com.amplifyframework.AmplifyException;
23-
2422
import org.json.JSONException;
2523
import org.json.JSONObject;
2624

@@ -31,25 +29,21 @@
3129
* A utility to read resource files.
3230
*/
3331
public final class Resources {
34-
35-
private Resources() { }
32+
private Resources() {}
3633

3734
/**
3835
* Obtains the raw resource ID for the given resource identifier.
3936
* @param context An Android Context
4037
* @param identifier Name of a raw resource
4138
* @return ID of the raw resource
42-
* @throws AmplifyException if the specified raw resource does not exist
39+
* @throws ResourceLoadingException if the specified raw resource does not exist
4340
*/
4441
@RawRes
45-
public static int getRawResourceId(Context context, String identifier) throws AmplifyException {
42+
public static int getRawResourceId(Context context, String identifier) throws ResourceLoadingException {
4643
try {
4744
return context.getResources().getIdentifier(identifier, "raw", context.getPackageName());
48-
} catch (Exception exception) {
49-
throw new AmplifyException(
50-
"Failed to find " + identifier + ".",
51-
exception, "Please check that it exists."
52-
);
45+
} catch (Exception lookupError) {
46+
throw new ResourceLoadingException("No such resource with identifier " + identifier, lookupError);
5347
}
5448
}
5549

@@ -58,9 +52,10 @@ public static int getRawResourceId(Context context, String identifier) throws Am
5852
* @param context An Android Context
5953
* @param identifier Name of a raw resource
6054
* @return JSON Object equivalent of the raw resource
61-
* @throws AmplifyException If resource with given ID does not exist or cannot be read
55+
* @throws ResourceLoadingException If resource with given ID does not exist or cannot be read
6256
*/
63-
public static JSONObject readJsonResource(Context context, String identifier) throws AmplifyException {
57+
public static JSONObject readJsonResource(Context context, String identifier)
58+
throws ResourceLoadingException {
6459
return readJsonResourceFromId(context, getRawResourceId(context, identifier));
6560
}
6661

@@ -69,18 +64,16 @@ public static JSONObject readJsonResource(Context context, String identifier) th
6964
* @param context An Android Context
7065
* @param resourceId ID of a raw resource
7166
* @return JSON Object equivalent of the raw resource
72-
* @throws AmplifyException If resource with given ID does not exist or cannot be read
67+
* @throws ResourceLoadingException If resource with given ID does not exist or cannot be read
7368
*/
74-
public static JSONObject readJsonResourceFromId(Context context, @RawRes int resourceId) throws AmplifyException {
69+
public static JSONObject readJsonResourceFromId(Context context, @RawRes int resourceId)
70+
throws ResourceLoadingException {
7571
InputStream inputStream;
7672

7773
try {
7874
inputStream = context.getResources().openRawResource(resourceId);
79-
} catch (NotFoundException exception) {
80-
throw new AmplifyException(
81-
"Failed to find the resource with ID " + resourceId + ".",
82-
exception, "Please check that it has been created."
83-
);
75+
} catch (NotFoundException notFoundError) {
76+
throw new ResourceLoadingException("No such resource with ID = " + resourceId, notFoundError);
8477
}
8578

8679
final Scanner in = new Scanner(inputStream);
@@ -92,11 +85,26 @@ public static JSONObject readJsonResourceFromId(Context context, @RawRes int res
9285

9386
try {
9487
return new JSONObject(sb.toString());
95-
} catch (JSONException jsonError) {
96-
throw new AmplifyException(
97-
"Failed to read the resource with ID " + resourceId + ".",
98-
jsonError, "Please check that it is correctly formed."
88+
} catch (JSONException badJsonError) {
89+
throw new ResourceLoadingException(
90+
"Failed to read the resource with ID " + resourceId + ".", badJsonError
9991
);
10092
}
10193
}
94+
95+
/**
96+
* Indicates that a requested resource cannot be loaded.
97+
*/
98+
public static final class ResourceLoadingException extends Exception {
99+
private static final long serialVersionUID = 1;
100+
101+
/**
102+
* Constructs a new resource loading exception.
103+
* @param failureRationale A rationale for the failure
104+
* @param rootCause A root cause for the failure
105+
*/
106+
public ResourceLoadingException(String failureRationale, Throwable rootCause) {
107+
super(failureRationale, rootCause);
108+
}
109+
}
102110
}

core/src/main/java/com/amplifyframework/devmenu/EnvironmentInfo.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ public String getPluginVersions() {
7676
*/
7777
public String getDeveloperEnvironmentInfo(@NonNull Context context) throws AmplifyException {
7878
Context appContext = Objects.requireNonNull(context).getApplicationContext();
79-
JSONObject envInfo = Resources.readJsonResource(appContext, DEV_ENV_INFO_FILE_NAME);
79+
final JSONObject envInfo;
80+
try {
81+
envInfo = Resources.readJsonResource(appContext, DEV_ENV_INFO_FILE_NAME);
82+
} catch (Resources.ResourceLoadingException resourceLoadingException) {
83+
throw new AmplifyException(
84+
"Failed to find " + DEV_ENV_INFO_FILE_NAME + ".", resourceLoadingException,
85+
"Please ensure it is present in your project."
86+
);
87+
}
8088
StringBuilder formattedEnvInfo = new StringBuilder();
8189
for (String envItem : devEnvironmentItems.keySet()) {
8290
if (envInfo.has(envItem)) {

0 commit comments

Comments
 (0)