Listener

@objc(MoEngageAuthenticationErrorListener)
public protocol Listener

Protocol for receiving authentication error notifications.

Implement this protocol to receive callbacks when authentication errors occur during network requests. The listener provides a way to handle authentication failures and implement appropriate recovery strategies.

Implementation Guidelines

Thread Safety

  • Listener methods are called serially
  • Ensure your implementation is thread-safe
  • Avoid blocking operations in callback methods

Error Handling

  • Implement specific handling for different error codes
  • Consider user experience when handling errors
  • Log errors for debugging and analytics

Recovery Strategies

  • Implement token refresh for expiration errors
  • Provide re-authentication for signature failures
  • Handle missing tokens by requesting new ones

Usage Example

class AuthErrorHandler: MoEngageAuthenticationError.Listener {
    func onError(_ error: MoEngageAuthenticationError) {
        // Log error for analytics
        Analytics.logAuthError(error.code.rawValue, error.message)

        // Handle JWT-specific errors
        if let jwtError = error as? MoEngageJwtAuthenticationError {
            handleJwtError(jwtError)
        }
    }

    private func handleJwtError(_ error: MoEngageJwtAuthenticationError) {
        // Handle based on specific JWT error code
        switch error.details.code {
        case .timeConstraintFailure:
            handleTokenExpiration(error)
        case .invalidSignature:
            handleSignatureFailure(error)
        case .tokenNotAvailable:
            handleMissingToken(error)
        default:
            handleGenericError(error)
        }
    }
}

Registration

Register your listener using the SDK’s registration methods:

let errorHandler = AuthErrorHandler()
MoEngageSDKCore.sharedInstance.registerAuthenticationListener(errorHandler)

Lifecycle Management

  • Listeners are held with strong references
  • Ensure proper cleanup when no longer needed
  • Consider using weak references in your implementation to avoid retain cycles
  • Called when an authentication error occurs.

    This method is invoked whenever authentication fails during network requests. Implement this method to handle authentication errors and implement appropriate recovery strategies.

    Error Handling Best Practices

    1. Log for Analytics: Always log errors for debugging and monitoring
    2. User Experience: Handle errors gracefully without disrupting user flow
    3. Recovery Strategies: Implement automatic recovery where possible
    4. Performance: Keep error handling lightweight and non-blocking

    Thread Safety

    • This method is called serially on the SDK’s internal queue
    • Ensure your implementation is thread-safe
    • Dispatch UI updates to the main queue if needed

    Common Implementation Patterns

    func onError(_ error: MoEngageAuthenticationError) {
        // 1. Log the error
        logger.error("Auth Error: \(error.code.rawValue) - \(error.message ?? "No message")")
    
        // 2. Handle JWT-specific errors
        if let jwtError = error as? MoEngageJwtAuthenticationError {
            handleJwtAuthError(jwtError)
        }
    }
    
    private func handleJwtAuthError(_ error: MoEngageJwtAuthenticationError) {
        // Handle based on specific JWT error code
        switch error.details.code {
        case .timeConstraintFailure:
            // Auto-refresh expired tokens
            refreshTokenAutomatically()
        case .tokenNotAvailable:
            // Request user to re-authenticate
            requestReAuthentication()
        default:
            // Generic error handling
            showErrorToUser(error)
        }
    }
    

    Note

    Keep this method implementation lightweight to avoid blocking the SDK

    Declaration

    Swift

    @objc
    func onError(_ error: MoEngageAuthenticationError)

    Parameters

    error

    The authentication error containing detailed information about the failure, including error code, message, and account metadata