firebase / firebase/firebase-ios-sdk

[Auth]Unable to fully receive errors from blocking function

Open
#14,482 3 comments 0 reactions 1 assignee Claimed by @ncooke3 View on GitHub
api: auth needs-attention
Dominant language
C++
Stars
6.7k
Forks
1.8k
Avg merge
2d 18h
Merged PRs (30d)
75

Description

### Description

Thanks to #14280, we can now receive the [HttpsError](https://firebase.google.com/docs/reference/functions/2nd-gen/python/firebase_functions.https_fn?_gl=1*ctwpew*_up*MQ..*_ga*MTEzNTI5NjIxLjE3NDAyMzA5MTE.*_ga_CW55HF8NVT*MTc0MDIzMDkxMS4xLjAuMTc0MDIzMDkyOS4wLjAuMA..#exceptions) 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.

https://github.com/firebase/firebase-ios-sdk/blob/b888e1e35f1b07d737d6d4be1b02d96b530a9a77/FirebaseAuth/Sources/Swift/Utilities/AuthErrorUtils.swift#L563-L573

To include all `HttpsError` information, set it in userInfo.

```swift
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)
```py
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
```swift
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.