mirror of
https://github.com/zhigang1992/expo.git
synced 2026-04-29 18:57:11 +08:00
Update Android client to send Expo-JSON-Error header and display nicer errors during experience load
- Update ExponentManifest to parse a JSON response and send the Expo-JSON-Error header - Update ManifestException's `toString()` method to output a nicer error for different cases. fbshipit-source-id: 9270275
This commit is contained in:
committed by
Exponent GitHub Bot
parent
7ac6cf8ccc
commit
8edb5c92c0
@@ -188,6 +188,7 @@ public class ExponentManifest {
|
||||
// Fetch manifest
|
||||
Request.Builder requestBuilder = ExponentUrls.addExponentHeadersToUrl(httpManifestUrl, manifestUrl.equals(Constants.INITIAL_URL));
|
||||
requestBuilder.header("Exponent-Accept-Signature", "true");
|
||||
requestBuilder.header("Expo-JSON-Error", "true");
|
||||
|
||||
Analytics.markEvent(Analytics.TimedEvent.STARTED_MANIFEST_NETWORK_REQUEST);
|
||||
if (Constants.DEBUG_MANIFEST_METHOD_TRACING) {
|
||||
@@ -213,7 +214,14 @@ public class ExponentManifest {
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) {
|
||||
if (!response.isSuccessful()) {
|
||||
listener.onError(new ManifestException(null, manifestUrl));
|
||||
ManifestException exception;
|
||||
try {
|
||||
final JSONObject errorJSON = new JSONObject(response.body().string());
|
||||
exception = new ManifestException(null, manifestUrl, errorJSON);
|
||||
} catch (JSONException | IOException e) {
|
||||
exception = new ManifestException(null, manifestUrl);
|
||||
}
|
||||
listener.onError(exception);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,16 +2,29 @@
|
||||
|
||||
package host.exp.exponent.exceptions;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import host.exp.exponent.Constants;
|
||||
import host.exp.expoview.ExpoViewBuildConfig;
|
||||
|
||||
public class ManifestException extends ExponentException {
|
||||
|
||||
private String mManifestUrl;
|
||||
private JSONObject mErrorJSON;
|
||||
|
||||
public ManifestException(final Exception originalException, final String manifestUrl) {
|
||||
super(originalException);
|
||||
mManifestUrl = manifestUrl;
|
||||
mErrorJSON = null;
|
||||
}
|
||||
|
||||
public ManifestException(final Exception originalException, final String manifestUrl, final JSONObject errorJSON) {
|
||||
super(originalException);
|
||||
mErrorJSON = errorJSON;
|
||||
mManifestUrl = manifestUrl;
|
||||
}
|
||||
|
||||
@@ -29,7 +42,42 @@ public class ManifestException extends ExponentException {
|
||||
//
|
||||
return "Could not load app." + extraMessage;
|
||||
} else {
|
||||
return "Could not load " + mManifestUrl + "." + extraMessage;
|
||||
String formattedMessage = "Could not load " + mManifestUrl + "." + extraMessage;
|
||||
if (mErrorJSON != null) {
|
||||
try {
|
||||
String errorCode = mErrorJSON.getString("errorCode");
|
||||
String rawMessage = mErrorJSON.getString("message");
|
||||
switch(errorCode) {
|
||||
case "EXPERIENCE_NOT_FOUND": // Really doesn't exist
|
||||
case "EXPERIENCE_NOT_PUBLISHED_ERROR": // Not published
|
||||
case "EXPERIENCE_RELEASE_NOT_FOUND_ERROR": // Can't find a release for the requested release channel
|
||||
formattedMessage = "No experience found at " + mManifestUrl + ".";
|
||||
break;
|
||||
case "EXPERIENCE_SDK_VERSION_OUTDATED":
|
||||
JSONObject metadata = mErrorJSON.getJSONObject("metadata");
|
||||
JSONArray availableSDKVersions = metadata.getJSONArray("availableSDKVersions");
|
||||
String sdkVersionRequired = availableSDKVersions.getString(0);
|
||||
formattedMessage = "This experience uses SDK v" + sdkVersionRequired + " , but this Expo client requires at least v" + Constants.SDK_VERSIONS_LIST.get(Constants.SDK_VERSIONS_LIST.size() - 1) + ".";
|
||||
break;
|
||||
case "EXPERIENCE_SDK_VERSION_TOO_NEW":
|
||||
formattedMessage = "This experience requires a newer version of the Expo client - please download the latest version from the Play Store.";
|
||||
break;
|
||||
case "USER_SNACK_NOT_FOUND":
|
||||
case "SNACK_NOT_FOUND":
|
||||
formattedMessage = "No snack found at " + mManifestUrl + ".";
|
||||
break;
|
||||
case "SNACK_RUNTIME_NOT_RELEASED":
|
||||
formattedMessage = rawMessage; // From server: `The Snack runtime for corresponding sdk version of this Snack ("${sdkVersions[0]}") is not released.`,
|
||||
break;
|
||||
case "SNACK_NOT_FOUND_FOR_SDK_VERSION":
|
||||
formattedMessage = rawMessage; // From server: `The snack "${fullName}" was found, but wasn't released for platform "${platform}" and sdk version "${sdkVersions[0]}".`
|
||||
break;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
return formattedMessage;
|
||||
}
|
||||
}
|
||||
return formattedMessage;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user