Skip to content

Commit

Permalink
fix: add AuthErrorConvertible conformance to CommonRuntimeError (#3287)
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian authored Oct 10, 2023
1 parent 6117f76 commit a76284a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import AwsCommonRuntimeKit
import AwsCIo
import AwsCHttp

private let connectivityErrorCodes: Set<UInt32> = [
AWS_ERROR_HTTP_CONNECTION_CLOSED.rawValue,
AWS_ERROR_HTTP_SERVER_CLOSED.rawValue,
AWS_IO_DNS_INVALID_NAME.rawValue,
AWS_IO_DNS_NO_ADDRESS_FOR_HOST.rawValue,
AWS_IO_DNS_QUERY_FAILED.rawValue,
AWS_IO_SOCKET_CONNECT_ABORTED.rawValue,
AWS_IO_SOCKET_CONNECTION_REFUSED.rawValue,
AWS_IO_SOCKET_CLOSED.rawValue,
AWS_IO_SOCKET_NETWORK_DOWN.rawValue,
AWS_IO_SOCKET_NO_ROUTE_TO_HOST.rawValue,
AWS_IO_SOCKET_NOT_CONNECTED.rawValue,
AWS_IO_SOCKET_TIMEOUT.rawValue,
AWS_IO_TLS_NEGOTIATION_TIMEOUT.rawValue,
UInt32(AWS_HTTP_STATUS_CODE_408_REQUEST_TIMEOUT.rawValue)
]

extension CommonRunTimeError: AuthErrorConvertible {
var authError: AuthError {
let error: CRTError
switch self { case .crtError(let crtError): error = crtError }

if connectivityErrorCodes.contains(UInt32(error.code)) {
return .service(error.name, error.message, AWSCognitoAuthError.network)
} else {
return .unknown("\(error.name) - \(error.message)", self)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ class AuthenticationProviderDeleteUserTests: BasePluginTest {
/// - I should get a .service error with .network as underlying error
///
func testOfflineDeleteUser() async throws {
// TODO: How are client side retry errors now modeled?
throw XCTSkip()
mockIdentityProvider = MockIdentityProvider(
mockRevokeTokenResponse: { _ in
try await RevokeTokenOutputResponse(httpResponse: .init(body: .empty, statusCode: .ok))
Expand Down Expand Up @@ -113,8 +111,6 @@ class AuthenticationProviderDeleteUserTests: BasePluginTest {
/// - I should get a valid response for the second call
///
func testOfflineDeleteUserAndRetry() async throws {
// TODO: How are client side retry errors now modeled?
throw XCTSkip()
mockIdentityProvider = MockIdentityProvider(
mockRevokeTokenResponse: { _ in
try await RevokeTokenOutputResponse(httpResponse: .init(body: .empty, statusCode: .ok))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class AuthDeleteUserTests: AWSAuthBaseTest {
Should produce .service error with underlyingError of .userNotFound || .limitExceed
Received: \(error)
""")

}
} catch {
XCTFail("Expected AuthError - received: \(error)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ extension ClientRuntime.HttpResponse {

convenience init(httpURLResponse: HTTPURLResponse, data: Data) throws {
let headers = Self.headers(from: httpURLResponse.allHeaderFields)
// TODO: double check if this works as expected
// Previously this needed to be `HttpBody.stream()`
let body = HttpBody.data(data)

guard let statusCode = HttpStatusCode(rawValue: httpURLResponse.statusCode) else {
Expand Down

0 comments on commit a76284a

Please sign in to comment.