Skip to content

Commit

Permalink
fix(Auth): Add underlying error details to session error (#3364)
Browse files Browse the repository at this point in the history
* fix(Auth): Add underlying error details to session error

* updated unit test target

* updating error message
  • Loading branch information
harsh62 authored Nov 15, 2023
1 parent a659848 commit 7b507f3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ struct InformSessionError: Action {
switch error {
case .service(let serviceError):
if isNotAuthorizedError(serviceError) {
event = .init(eventType: .throwError(.sessionExpired))
event = .init(eventType: .throwError(
.sessionExpired(error: serviceError)))
} else {
event = .init(eventType: .receivedSessionError(error))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
forceRefresh: forceRefresh)

case .error(let error):
if case .sessionExpired = error {
if case .sessionExpired(let error) = error {
log.verbose("Session is expired")
let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession()
let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession(
underlyingError: error)
return session
} else if case .sessionError(_, let credentials) = error {
return try await refreshIfRequired(
Expand Down Expand Up @@ -125,8 +126,9 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
return try sessionResultWithFetchError(fetchError,
authenticationState: authenticationState,
existingCredentials: credentials)
case .sessionExpired:
let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession()
case .sessionExpired(let error):
let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession(
underlyingError: error)
return session
default:
let message = "Unknown error occurred"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ enum AuthorizationError: Error {
case service(error: Swift.Error)
case invalidState(message: String)
case sessionError(FetchSessionError, AmplifyCredentials)
case sessionExpired
case sessionExpired(error: Error)
}

extension AuthorizationError: AuthErrorConvertible {
var authError: AuthError {
switch self {
case .sessionExpired:
return .sessionExpired("", "", nil)
case .sessionExpired(let error):
return .sessionExpired(
"Session expired",
"Invoke Auth.signIn to re-authenticate the user",
error)
case .configuration(let message):
return .configuration(message, "")
case .service(let error):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ struct AuthCognitoSignedInSessionHelper {
return authSession
}

static func makeExpiredSignedInSession() -> AWSAuthCognitoSession {
static func makeExpiredSignedInSession(underlyingError: Error) -> AWSAuthCognitoSession {
let identityIdError = AuthError.sessionExpired(
AuthPluginErrorConstants.identityIdSessionExpiredError.errorDescription,
AuthPluginErrorConstants.identityIdSessionExpiredError.recoverySuggestion)
AuthPluginErrorConstants.identityIdSessionExpiredError.recoverySuggestion,
underlyingError)

let awsCredentialsError = AuthError.sessionExpired(
AuthPluginErrorConstants.awsCredentialsSessionExpiredError.errorDescription,
AuthPluginErrorConstants.awsCredentialsSessionExpiredError.recoverySuggestion)
AuthPluginErrorConstants.awsCredentialsSessionExpiredError.recoverySuggestion,
underlyingError)

let tokensError = AuthError.sessionExpired(
AuthPluginErrorConstants.cognitoTokensSessionExpiredError.errorDescription,
AuthPluginErrorConstants.cognitoTokensSessionExpiredError.recoverySuggestion)
AuthPluginErrorConstants.cognitoTokensSessionExpiredError.recoverySuggestion,
underlyingError)

let authSession = AWSAuthCognitoSession(isSignedIn: true,
identityIdResult: .failure(identityIdError),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class AWSAuthFederationToIdentityPoolTests: BaseAuthorizationTests {
AuthorizationState.configured),
AuthState.configured(
AuthenticationState.signedOut(.testData),
AuthorizationState.error(.sessionExpired))
AuthorizationState.error(.sessionExpired(
error: NotAuthorizedException(message: "message"))))
]

for initialState in statesToTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ extension FetchAuthSessionState: Codable {

extension AuthorizationError: Codable {
public init(from decoder: Decoder) throws {
self = .sessionExpired
self = .sessionExpired(error: NotAuthorizedException(message: "message"))
}

public func encode(to encoder: Encoder) throws {
Expand Down

0 comments on commit 7b507f3

Please sign in to comment.