Skip to content

[Auth]Unable to fully receive errors from blocking function #14482

Open
@auto-ororo

Description

@auto-ororo

Description

Thanks to #14280, we can now receive the HttpsError from Blocking Functions.However, we still cannot obtain any information beyond the HttpsError.message.For example, fields such as HttpsError.code and HttpsError.details remain unavailable. It would be preferable to retrieve all error details, similar to what the Android SDK offers.

Suggested Fix

The current code only retrieves HttpsError.message from the server response.

static func blockingCloudFunctionServerResponse(message: String?) -> Error {
guard let message else {
return error(code: .blockingCloudFunctionError, message: message)
}
guard let jsonDict = extractJSONObjectFromString(from: message) else {
return error(code: .blockingCloudFunctionError, message: message)
}
let errorDict = jsonDict["error"] as? [String: Any] ?? [:]
let errorMessage = errorDict["message"] as? String
return error(code: .blockingCloudFunctionError, message: errorMessage)
}

To include all HttpsError information, set it in userInfo.

static func blockingCloudFunctionServerResponse(message: String?) -> Error {
  guard let message else {
    return error(code: .blockingCloudFunctionError, message: message)
  }
  guard let jsonDict = extractJSONObjectFromString(from: message) else {
    return error(code: .blockingCloudFunctionError, message: message)
  }
  let httpsError = jsonDict["error"] as? [String: Any] ?? [:]
  var userInfo: [String: Any] = [:]
  userInfo[AuthErrors.userInfoBlockingFunctionHttpErrorKey] = httpsError
  let errorMessage = httpsError["message"] as? String
  userInfo[NSLocalizedDescriptionKey] = errorMessage // ※ For backwards compatible
  
  return error(code: .blockingCloudFunctionError, userInfo: userInfo)
}

Reproducing the issue

  1. Throw below httpsError in your beforeUserCreated Blocking Cloud Function (can be any blocking function)
    https_fn.HttpsError(
         code=https_fn.FunctionsErrorCode.UNAUTHENTICATED,
         message="authentication failed",
         details= {
             "type": "mfa",
         },
     )
  2. Create a user in the swift app and catch the error, here's an example code
    do {
         try await Auth.auth().createUser(withEmail: email, password: password)
    } catch {
         print("\(error)")
    }
    • You catch below error and this error do not contain HttpsError.code or HttpsError.details
      Error Domain=FIRAuthErrorDomain Code=17105 "authentication failed" UserInfo={NSLocalizedDescription=authentication failed, FIRAuthErrorUserInfoNameKey=ERROR_BLOCKING_CLOUD_FUNCTION_RETURNED_ERROR}
      
    • In Android SDK, you can catch error including HttpsError.code(status) and HttpsError.details
      BLOCKING_FUNCTION_ERROR_RESPONSE:HTTP Cloud Function returned an error: {"error":{"details":{"type":"mfa"},"message":"authentication failed","status":"UNAUTHENTICATED"}}
      

Firebase SDK Version

11.8.1

Xcode Version

16.2

Installation Method

Swift Package Manager

Firebase Product(s)

Authentication, Cloud Function

Targeted Platforms

iOS

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions