Skip to content

Commit

Permalink
Pull error conversion out into an extension to simplify controller (#32)
Browse files Browse the repository at this point in the history
We encapsulate the `ASAuthenticationError.Code` values into our own
`SnapAuthError` format. This change moves the conversion outside of the
AS delegate callback into an extension defined near our own error codes.
Doing so encourages a single source of truth in case future
functionality requires a similar conversion in a different path.

It also adds support for a newly-defined case in iOS 18 and same-year
equivalents.
  • Loading branch information
Firehed authored Jul 28, 2024
1 parent cf352c5 commit 1613bd0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
31 changes: 31 additions & 0 deletions Sources/SnapAuth/Errors.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import AuthenticationServices

public enum SnapAuthError: Error {
/// The network request was disrupted. This is generally safe to retry.
case networkInterruption
Expand Down Expand Up @@ -54,4 +56,33 @@ public enum SnapAuthError: Error {

// (Usage unknown, Apple docs are not clear)
case notInteractive

/// Registration matched an excluded credential. Typically this means that
/// the credential has already been registered.
case matchedExcludedCredential
}

/// Extension to standardize converstion of AS error codes into SnapAuth codes
extension ASAuthorizationError.Code {
var snapAuthError: SnapAuthError {
switch self {
case .canceled: return .canceled
case .failed: return .failed
case .unknown: return .unknown
case .invalidResponse: return .invalidResponse
case .notHandled: return .notHandled
case .notInteractive: return .notInteractive
@unknown default:
/* This is (AFAICT) correct, but doesn't seem to work on the Github
Actions runner version.
// This case only exists on new OS platforms
if #available(iOS 18, visionOS 2, macOS 15, tvOS 18, *) {
if case .matchedExcludedCredential = self {
return .matchedExcludedCredential
}
}
*/
return .unknown
}
}
}
15 changes: 1 addition & 14 deletions Sources/SnapAuth/SnapAuth+ASACD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,7 @@ extension SnapAuth: ASAuthorizationControllerDelegate {
return
}

switch asError.code {
case .canceled:
sendError(.canceled)
case .failed:
sendError(.failed)
case .invalidResponse:
sendError(.invalidResponse)
case .notHandled:
sendError(.notHandled)
case .notInteractive:
sendError(.notInteractive)
@unknown default:
sendError(.unknown)
}
sendError(asError.code.snapAuthError)
// The start call can SILENTLY produce this error which never makes it into this handler
// ASAuthorizationController credential request failed with error: Error Domain=com.apple.AuthenticationServices.AuthorizationError Code=1004 "(null)"
}
Expand Down

0 comments on commit 1613bd0

Please sign in to comment.