diff --git a/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/AWSLocationGeoPlugin+ClientBehavior.swift b/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/AWSLocationGeoPlugin+ClientBehavior.swift index 8fc5ec2d33..f79c09981f 100644 --- a/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/AWSLocationGeoPlugin+ClientBehavior.swift +++ b/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/AWSLocationGeoPlugin+ClientBehavior.swift @@ -97,9 +97,14 @@ extension AWSLocationGeoPlugin { country: $0.country) } return places + } catch let error as GeoErrorConvertible { + throw error.geoError } catch { - let geoError = GeoErrorHelper.mapAWSLocationError(error) - throw geoError + throw Geo.Error.unknown( + error.localizedDescription, + "See underlying error.", + error + ) } } @@ -167,9 +172,14 @@ extension AWSLocationGeoPlugin { country: $0.country) } return places + } catch let error as GeoErrorConvertible { + throw error.geoError } catch { - let geoError = GeoErrorHelper.mapAWSLocationError(error) - throw geoError + throw Geo.Error.unknown( + error.localizedDescription, + "See underlying error.", + error + ) } } diff --git a/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/AWSLocationGeoPlugin+Configure.swift b/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/AWSLocationGeoPlugin+Configure.swift index c210cdd23f..d1a4caf9ae 100644 --- a/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/AWSLocationGeoPlugin+Configure.swift +++ b/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/AWSLocationGeoPlugin+Configure.swift @@ -30,10 +30,11 @@ extension AWSLocationGeoPlugin { let authService = AWSAuthService() let credentialsProvider = authService.getCredentialsProvider() let region = configuration.regionName + // TODO: FrameworkMetadata Replacement let serviceConfiguration = try LocationClient.LocationClientConfiguration( - credentialsProvider: credentialsProvider, - frameworkMetadata: AmplifyAWSServiceConfiguration.frameworkMetaData(), - region: region) + region: region, + credentialsProvider: credentialsProvider + ) #if os(iOS) || os(macOS) // no-op #else diff --git a/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/Support/Utils/GeoErrorConvertible.swift b/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/Support/Utils/GeoErrorConvertible.swift new file mode 100644 index 0000000000..ad35aea33a --- /dev/null +++ b/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/Support/Utils/GeoErrorConvertible.swift @@ -0,0 +1,79 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Amplify +import Foundation +import AWSLocation +import AWSClientRuntime + +protocol GeoErrorConvertible { + var geoError: Geo.Error { get } +} + +extension AWSLocation.AccessDeniedException: GeoErrorConvertible { + var geoError: Geo.Error { + .accessDenied( + message ?? "", + GeoPluginErrorConstants.accessDenied, + self + ) + } +} + +extension AWSLocation.InternalServerException: GeoErrorConvertible { + var geoError: Geo.Error { + .serviceError( + message ?? "", + GeoPluginErrorConstants.internalServer, + self + ) + } +} + +extension AWSLocation.ResourceNotFoundException: GeoErrorConvertible { + var geoError: Geo.Error { + .serviceError( + message ?? "", + GeoPluginErrorConstants.resourceNotFound, + self + ) + } +} + +extension AWSLocation.ThrottlingException: GeoErrorConvertible { + var geoError: Geo.Error { + .serviceError( + message ?? "", + GeoPluginErrorConstants.throttling, + self + ) + } +} + +extension AWSLocation.ValidationException: GeoErrorConvertible { + var geoError: Geo.Error { + .serviceError( + message ?? "", + GeoPluginErrorConstants.validation, + self + ) + } +} + +extension AWSClientRuntime.UnknownAWSHTTPServiceError: GeoErrorConvertible { + var geoError: Geo.Error { + .unknown( + """ + Unknown service error occured with: + - status: \(httpResponse.statusCode) + - message: \(message ?? "") + """, + "", + self + ) + } +} diff --git a/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/Support/Utils/GeoErrorHelper.swift b/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/Support/Utils/GeoErrorHelper.swift deleted file mode 100644 index c21666a68e..0000000000 --- a/AmplifyPlugins/Geo/Sources/AWSLocationGeoPlugin/Support/Utils/GeoErrorHelper.swift +++ /dev/null @@ -1,63 +0,0 @@ -// -// Copyright Amazon.com Inc. or its affiliates. -// All Rights Reserved. -// -// SPDX-License-Identifier: Apache-2.0 -// - -import Amplify -import Foundation - -import AWSLocation - -class GeoErrorHelper { - static func getDefaultError(_ error: Error) -> Geo.Error { - return Geo.Error.unknown(error.localizedDescription, "See underlying error.", error) - } - - static func mapAWSLocationError(_ error: Error) -> Geo.Error { - let defaultError = GeoErrorHelper.getDefaultError(error) - - if let searchPlaceIndexForTextOutputError = error as? SearchPlaceIndexForTextOutputError { - return GeoErrorHelper.mapError(error: searchPlaceIndexForTextOutputError) ?? defaultError - } else if let searchPlaceIndexForPositionOutputError = error as? SearchPlaceIndexForPositionOutputError { - return GeoErrorHelper.mapError(error: searchPlaceIndexForPositionOutputError) ?? defaultError - } - - return defaultError - } - - static func mapError(error: SearchPlaceIndexForTextOutputError) -> Geo.Error? { - switch error { - case .accessDeniedException(let accessDeniedException): - return Geo.Error.accessDenied(accessDeniedException.message ?? "", GeoPluginErrorConstants.accessDenied, error) - case .internalServerException(let internalServerException): - return Geo.Error.serviceError(internalServerException.message ?? "", GeoPluginErrorConstants.internalServer, error) - case .resourceNotFoundException(let resournceNotFoundException): - return Geo.Error.serviceError(resournceNotFoundException.message ?? "", GeoPluginErrorConstants.resourceNotFound, error) - case .throttlingException(let throttlingException): - return Geo.Error.serviceError(throttlingException.message ?? "", GeoPluginErrorConstants.throttling, error) - case .validationException(let validationException): - return Geo.Error.serviceError(validationException.message ?? "", GeoPluginErrorConstants.validation, error) - case .unknown(let unknownAWSHttpServiceError): - return Geo.Error.unknown(unknownAWSHttpServiceError._message ?? "", "See underlying error.", error) - } - } - - static func mapError(error: SearchPlaceIndexForPositionOutputError) -> Geo.Error? { - switch error { - case .accessDeniedException(let accessDeniedException): - return Geo.Error.accessDenied(accessDeniedException.message ?? "", GeoPluginErrorConstants.accessDenied, error) - case .internalServerException(let internalServerException): - return Geo.Error.serviceError(internalServerException.message ?? "", GeoPluginErrorConstants.internalServer, error) - case .resourceNotFoundException(let resournceNotFoundException): - return Geo.Error.serviceError(resournceNotFoundException.message ?? "", GeoPluginErrorConstants.resourceNotFound, error) - case .throttlingException(let throttlingException): - return Geo.Error.serviceError(throttlingException.message ?? "", GeoPluginErrorConstants.throttling, error) - case .validationException(let validationException): - return Geo.Error.serviceError(validationException.message ?? "", GeoPluginErrorConstants.validation, error) - case .unknown(let unknownAWSHttpServiceError): - return Geo.Error.unknown(unknownAWSHttpServiceError._message ?? "", "See underlying error.", error) - } - } -} diff --git a/AmplifyPlugins/Geo/Tests/AWSLocationGeoPluginTests/Mocks/MockAWSClientConfiguration.swift b/AmplifyPlugins/Geo/Tests/AWSLocationGeoPluginTests/Mocks/MockAWSClientConfiguration.swift index 378d825711..ec24a7486d 100644 --- a/AmplifyPlugins/Geo/Tests/AWSLocationGeoPluginTests/Mocks/MockAWSClientConfiguration.swift +++ b/AmplifyPlugins/Geo/Tests/AWSLocationGeoPluginTests/Mocks/MockAWSClientConfiguration.swift @@ -14,56 +14,17 @@ import XCTest @testable import AWSLocationGeoPlugin @testable import AWSPluginsTestCommon -class MockAWSClientConfiguration: LocationClientConfigurationProtocol { - var encoder: ClientRuntime.RequestEncoder? - - var decoder: ClientRuntime.ResponseDecoder? - - var httpClientEngine: ClientRuntime.HttpClientEngine - - var httpClientConfiguration: ClientRuntime.HttpClientConfiguration - - var idempotencyTokenGenerator: ClientRuntime.IdempotencyTokenGenerator - - var clientLogMode: ClientRuntime.ClientLogMode - - var partitionID: String? - - var useFIPS: Bool? - - var useDualStack: Bool? - - var endpoint: String? - - var credentialsProvider: CredentialsProvider - - var region: String? - - var signingRegion: String? - - var endpointResolver: EndpointResolver - - var regionResolver: RegionResolver? - - var frameworkMetadata: FrameworkMetadata? - - var logger: LogAgent - - var retryer: SDKRetryer - - init(config: AWSLocationGeoPluginConfiguration) throws { - let defaultSDKRuntimeConfig = try DefaultSDKRuntimeConfiguration("MockAWSClientConfiguration") - - self.httpClientEngine = defaultSDKRuntimeConfig.httpClientEngine - self.httpClientConfiguration = defaultSDKRuntimeConfig.httpClientConfiguration - self.idempotencyTokenGenerator = defaultSDKRuntimeConfig.idempotencyTokenGenerator - self.clientLogMode = defaultSDKRuntimeConfig.clientLogMode - self.credentialsProvider = MockAWSAuthService().getCredentialsProvider() - self.region = config.regionName - self.signingRegion = "" - self.endpointResolver = MockEndPointResolver() - self.logger = MockLogAgent() - self.retryer = try SDKRetryer(options: RetryOptions(jitterMode: .default)) +extension LocationClient.LocationClientConfiguration { + static func mock(region: String) throws -> LocationClient.LocationClientConfiguration { + try .init( + region: region, + credentialsProvider: MockAWSAuthService().getCredentialsProvider(), + serviceSpecific: .init( + endpointResolver: MockEndPointResolver() + ), + signingRegion: "", + retryMode: .standard + ) } } diff --git a/AmplifyPlugins/Geo/Tests/AWSLocationGeoPluginTests/Mocks/MockAWSLocation.swift b/AmplifyPlugins/Geo/Tests/AWSLocationGeoPluginTests/Mocks/MockAWSLocation.swift index fe7a6cb9d8..0dc4080432 100644 --- a/AmplifyPlugins/Geo/Tests/AWSLocationGeoPluginTests/Mocks/MockAWSLocation.swift +++ b/AmplifyPlugins/Geo/Tests/AWSLocationGeoPluginTests/Mocks/MockAWSLocation.swift @@ -27,7 +27,8 @@ public class MockAWSLocation: AWSLocationBehavior { public init(pluginConfig: AWSLocationGeoPluginConfiguration) throws { self.locationClient = try LocationClient( - config: MockAWSClientConfiguration(config: pluginConfig)) + config: .mock(region: pluginConfig.regionName) + ) } public func getEscapeHatch() -> LocationClient {