Skip to content

Commit

Permalink
fix: Displaying the correct message on connectivity errors (#82)
Browse files Browse the repository at this point in the history
* chore: Unpinning Amplify version

* fix: Displaying the correct message on connectivity errors

* chore: Updating CHANGELOG for release
  • Loading branch information
ruisebas authored Jun 7, 2024
1 parent 15e2d84 commit 9f21487
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 1.1.4 (2024-06-07)

### Bug Fixes
- **Authenticator**: Showing the proper message when there's connectivity issues (#82)

### Misc. Updates
- Updating code to support Amplify 2.35+. (#82)

## 1.1.3 (2024-06-04)

### Bug Fixes
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/aws-amplify/amplify-swift",
"state" : {
"revision" : "0e4dc695c4cd3c53d145b9f6498ad83004edae6d",
"version" : "2.34.4"
"revision" : "fa6ca64f42c3e4e72cf6b62c30bc45b2be7b05bc",
"version" : "2.35.1"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
targets: ["Authenticator"]),
],
dependencies: [
.package(url: "https://github.com/aws-amplify/amplify-swift", "2.16.0"..<"2.35.0"),
.package(url: "https://github.com/aws-amplify/amplify-swift", from: "2.35.0"),
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Authenticator/Models/AuthenticatorState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Amplify
@_spi(InternalAmplifyPluginExtension) import AWSCognitoAuthPlugin
@_spi(InternalAmplifyPluginExtension) import AWSPluginsCore
@_spi(InternalAmplifyPluginExtension) import InternalAmplifyCredentials
import Foundation

/// An `ObservableObject` that represents the Authenticator's state
Expand Down
20 changes: 20 additions & 0 deletions Sources/Authenticator/States/AuthenticatorBaseState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ public class AuthenticatorBaseState: ObservableObject {
}
}

// First check if the underlying error is a connectivity one
if isConnectivityError(error.underlyingError) {
log.verbose("The error is identified as a connectivity issue, displaying the corresponding localized string.")
return "authenticator.cognitoError.network".localized()
}

guard let cognitoError = error.underlyingError as? AWSCognitoAuthError else {
log.verbose("Unable to localize error that is not of type AWSCognitoAuthError")
return nil
Expand All @@ -229,6 +235,20 @@ public class AuthenticatorBaseState: ObservableObject {
log.verbose("No localizable string was found for error of type '\(cognitoError)'")
return nil
}

private func isConnectivityError(_ error: Error?) -> Bool {
guard let error = error as? NSError else {
return false
}
let networkErrorCodes = [
NSURLErrorCannotFindHost,
NSURLErrorCannotConnectToHost,
NSURLErrorNetworkConnectionLost,
NSURLErrorDNSLookupFailed,
NSURLErrorNotConnectedToInternet
]
return networkErrorCodes.contains(where: { $0 == error.code })
}
}

extension AuthenticatorBaseState: Equatable {
Expand Down

0 comments on commit 9f21487

Please sign in to comment.