fun checkResponseAndCreateError()

in facebook-core/src/main/java/com/facebook/FacebookRequestError.kt [295:386]


    fun checkResponseAndCreateError(
        singleResult: JSONObject,
        batchResult: Any?,
        connection: HttpURLConnection?
    ): FacebookRequestError? {
      try {
        if (singleResult.has(CODE_KEY)) {
          val responseCode = singleResult.getInt(CODE_KEY)
          val body =
              getStringPropertyAsJSON(
                  singleResult, BODY_KEY, GraphResponse.NON_JSON_RESPONSE_PROPERTY)
          if (body != null && body is JSONObject) {
            val jsonBody = body
            // Does this response represent an error from the service? We might get either
            // an "error" with several sub-properties, or else one or more top-level fields
            // containing error info.
            var errorType: String? = null
            var errorMessage: String? = null
            var errorUserMessage: String? = null
            var errorUserTitle: String? = null
            var errorIsTransient = false
            var errorCode = INVALID_ERROR_CODE
            var errorSubCode = INVALID_ERROR_CODE
            var hasError = false
            if (jsonBody.has(ERROR_KEY)) {
              // We assume the error object is correctly formatted.
              val error = getStringPropertyAsJSON(jsonBody, ERROR_KEY, null) as JSONObject?
              errorType = error?.optString(ERROR_TYPE_FIELD_KEY, null)
              errorMessage = error?.optString(ERROR_MESSAGE_FIELD_KEY, null)
              errorCode =
                  error?.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE) ?: INVALID_ERROR_CODE
              errorSubCode =
                  error?.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE) ?: INVALID_ERROR_CODE
              errorUserMessage = error?.optString(ERROR_USER_MSG_KEY, null)
              errorUserTitle = error?.optString(ERROR_USER_TITLE_KEY, null)
              errorIsTransient = error?.optBoolean(ERROR_IS_TRANSIENT_KEY, false) ?: false
              hasError = true
            } else if (jsonBody.has(ERROR_CODE_KEY) ||
                jsonBody.has(ERROR_MSG_KEY) ||
                jsonBody.has(ERROR_REASON_KEY)) {
              errorType = jsonBody.optString(ERROR_REASON_KEY, null)
              errorMessage = jsonBody.optString(ERROR_MSG_KEY, null)
              errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE)
              errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE)
              hasError = true
            }
            if (hasError) {
              return FacebookRequestError(
                  responseCode,
                  errorCode,
                  errorSubCode,
                  errorType,
                  errorMessage,
                  errorUserTitle,
                  errorUserMessage,
                  jsonBody,
                  singleResult,
                  batchResult,
                  connection,
                  null,
                  errorIsTransient)
            }
          }

          // If we didn't get error details, but we did get a failure response code, report
          // it.
          if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
            return FacebookRequestError(
                responseCode,
                INVALID_ERROR_CODE,
                INVALID_ERROR_CODE,
                null,
                null,
                null,
                null,
                if (singleResult.has(BODY_KEY)) {
                  getStringPropertyAsJSON(
                      singleResult, BODY_KEY, GraphResponse.NON_JSON_RESPONSE_PROPERTY) as
                      JSONObject?
                } else {
                  null
                },
                singleResult,
                batchResult,
                connection,
                null,
                false)
          }
        }
      } catch (e: JSONException) {}
      return null
    }