Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Support NS_ERROR_ENUM for better error handling in Swift #7723

Closed
liamnichols opened this issue Mar 15, 2021 · 4 comments
Closed

FR: Support NS_ERROR_ENUM for better error handling in Swift #7723

liamnichols opened this issue Mar 15, 2021 · 4 comments

Comments

@liamnichols
Copy link

[READ] Guidelines

For large or ambiguous requests, such as significant breaking changes or use
cases that could be addressed by multiple different features, consider instead
starting a
Pitch
discussion for ideas that you'd like to discuss with the Firebase community to
flush out.

When filing a feature request please make sure the issue title starts with "FR:".

A good feature request ideally

  • is either immediately obvious (i.e. "Add Sign in with Apple support"), or
  • starts with a use case that is not achievable with the existing Firebase API and
    includes an API proposal that would make the use case possible. The proposed API
    change does not need to be very specific.

Once you've read this section, please delete it and fill out the rest of the template.

Feature proposal

  • Firebase Component: All

👋 I'm trying to use the Firebase iOS SDK (in particular, FirebaseAuth) in a Swift project and I noticed that it was quite hard to check errors returned by the library methods.. For example, to switch over some common scenarios I ended up writing code a bit like this:

let nsError: NSError = error
if nsError.domain == AuthErrorDomain, let code = AuthErrorCode(rawValue: nsError.code) {
    switch code {
    case .invalidEmail:
        startInvalidEmailFlow()
        return
    default:
        break
    }
}
startGenericErrorFlow()

It's great that all of the constants have Swift names but I was really hoping that I could use errors in a more Swift friendly way. I dug back through some docs and realised that what we are missing is the NS_ERROR_ENUM in order to have the errors better declared in Swift.

Some related links:

https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md#importing-error-types-from-objective-c
https://developer.apple.com/documentation/swift/cocoa_design_patterns/handling_cocoa_errors_in_swift#2993724

If we declared FIRAuthErrorDomain using the NSErrorDomain typedef and switched FIRAuthErrorCode enum to an NS_ERROR_ENUM enum with the name FIRAuthError (drop the 'Code') then we could start using the errors like the following in Swift:

switch error {
case AuthError.invalidEmail:
    startInvalidEmailFlow()
default:
    startGenericErrorFlow()
}

I imagine that it's not such a simple task, but i believe that this would really help a lot of users handle errors much more reliably because right now the alternative options aren't great. Thanks!

@yuchenshi
Copy link
Member

I've tracked this feature request internally at b/183415773.

@a-25
Copy link
Contributor

a-25 commented Nov 23, 2021

@liamnichols, @yuchenshi: Hello.
Made a PR for it here - 07d55e1
But it contains breaking changes.

So the objective-c-style code:
if nsError.domain == AuthErrorDomain, let code = AuthErrorCode(rawValue: nsError.code)

should be converted into:
if nsError.domain == AuthErrorDomain, let code = AuthErrorCode.Code(rawValue: nsError.code)

And swift variant mentioned by @liamnichols is also working:

switch error {
case AuthErrorCode.invalidEmail:
    startInvalidEmailFlow()
default:
    startGenericErrorFlow()
}

Do you think it is OK to merge it or I'd better fix the breaking changes?

a-25 added a commit to a-25/firebase-ios-sdk that referenced this issue Nov 24, 2021
@paulb777
Copy link
Member

@a-25 Thanks for the analysis and PR!

We're currently ramping up on a project to modernize the Firebase Swift APIs and improving error handling should be a part of it.

I'll mark your PR for the next breaking change release, and we can also consider a more incremental approach like #9007 that fixes error handling in a Swift wrapper library.

@paulb777 paulb777 added this to the Firebase 9 milestone Nov 24, 2021
ryanwilson pushed a commit that referenced this issue Mar 31, 2022
* Added NS_ERROR_ENUM to Firebase Auth, breaking changes (#7723)

* Added NS_ERROR_ENUM to Firebase Auth, breaking changes (#7723), code style
@ryanwilson
Copy link
Member

Fixed in the Firebase 9 branch which will be merged to master in the near future. Closing now, thanks!

paulb777 pushed a commit that referenced this issue Apr 4, 2022
* Added NS_ERROR_ENUM to Firebase Auth, breaking changes (#7723)

* Added NS_ERROR_ENUM to Firebase Auth, breaking changes (#7723), code style
@firebase firebase locked and limited conversation to collaborators May 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants