1 // Copyright 2015-present 650 Industries. All rights reserved.
2 package host.exp.exponent.exceptions
3 
4 import host.exp.exponent.Constants
5 import host.exp.expoview.ExpoViewBuildConfig
6 import org.json.JSONException
7 import org.json.JSONObject
8 import java.lang.Exception
9 
10 class ManifestException : ExponentException {
11   private val manifestUrl: String
12   private var errorJSON: JSONObject? = null
13 
14   constructor(originalException: Exception?, manifestUrl: String) : super(originalException) {
15     this.manifestUrl = manifestUrl
16     this.errorJSON = null
17   }
18 
19   constructor(originalException: Exception?, manifestUrl: String, errorJSON: JSONObject?) : super(
20     originalException
21   ) {
22     this.errorJSON = errorJSON
23     this.manifestUrl = manifestUrl
24   }
25 
26   override fun toString(): String {
27     val extraMessage = if (ExpoViewBuildConfig.DEBUG) {
28       // This will get hit in a detached app.
29       " Are you sure expo-cli is running?"
30     } else {
31       ""
32     }
33 
34     return when (manifestUrl) {
35       Constants.INITIAL_URL -> "Could not load app.$extraMessage"
36       else -> {
37         var formattedMessage = "Could not load $manifestUrl.$extraMessage"
38         if (errorJSON != null) {
39           try {
40             val errorCode = errorJSON!!.getString("errorCode")
41             val rawMessage = errorJSON!!.getString("message")
42             when (errorCode) {
43               "EXPERIENCE_NOT_FOUND", // Really doesn't exist
44               "EXPERIENCE_NOT_PUBLISHED_ERROR", // Not published
45               "EXPERIENCE_RELEASE_NOT_FOUND_ERROR" -> // Can't find a release for the requested release channel
46                 formattedMessage =
47                   "No project found at $manifestUrl."
48               "EXPERIENCE_SDK_VERSION_OUTDATED" -> {
49                 val metadata = errorJSON!!.getJSONObject("metadata")
50                 val availableSDKVersions = metadata.getJSONArray("availableSDKVersions")
51                 val sdkVersionRequired = availableSDKVersions.getString(0)
52                 formattedMessage =
53                   "This project uses SDK " + sdkVersionRequired + " , but this version of Expo Go only supports the following SDKs: " + Constants.SDK_VERSIONS_LIST.joinToString() + ". To load the project, it must be updated to a supported SDK version or an older version of Expo Go must be used."
54               }
55               "NO_SDK_VERSION_SPECIFIED" -> {
56                 formattedMessage = "Incompatible SDK version or no SDK version specified. This version of Expo Go only supports the following SDKs (runtimes): " + Constants.SDK_VERSIONS_LIST.joinToString() + ". A development build must be used to load other runtimes."
57               }
58               "EXPERIENCE_SDK_VERSION_TOO_NEW" ->
59                 formattedMessage =
60                   "This project requires a newer version of Expo Go - please download the latest version from the Play Store."
61               "EXPERIENCE_NOT_VIEWABLE" ->
62                 formattedMessage =
63                   rawMessage // From server: The experience you requested is not viewable by you. You will need to log in or ask the owner to grant you access.
64               "USER_SNACK_NOT_FOUND", "SNACK_NOT_FOUND" ->
65                 formattedMessage =
66                   "No snack found at $manifestUrl."
67               "SNACK_RUNTIME_NOT_RELEASED" ->
68                 formattedMessage =
69                   rawMessage // From server: `The Snack runtime for corresponding sdk version of this Snack ("${sdkVersions[0]}") is not released.`,
70               "SNACK_NOT_FOUND_FOR_SDK_VERSION" ->
71                 formattedMessage =
72                   rawMessage // From server: `The snack "${fullName}" was found, but wasn't released for platform "${platform}" and sdk version "${sdkVersions[0]}".`
73             }
74           } catch (e: JSONException) {
75             return formattedMessage
76           }
77         }
78         formattedMessage
79       }
80     }
81   }
82 }
83