Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Smithy Waiters #755

Merged
merged 4 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion AWSClientRuntime/Sources/Errors/UnknownAWSHttpServiceError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import ClientRuntime

/// AWS specific Service Error structure used when exact error could not be deduced from the `HttpResponse`
public struct UnknownAWSHttpServiceError: AWSHttpServiceError, Equatable {
/// The error type for this error, or `nil` if the type is not known.
public var _errorType: String?

public var _isThrottling: Bool = false

public var _statusCode: HttpStatusCode?
Expand All @@ -23,10 +26,30 @@ public struct UnknownAWSHttpServiceError: AWSHttpServiceError, Equatable {
}

extension UnknownAWSHttpServiceError {
public init(httpResponse: HttpResponse, message: String? = nil, requestID: String? = nil) {

/// Creates an `UnknownAWSHttpServiceError` from a `HttpResponse` and associated parameters.
/// - Parameters:
/// - httpResponse: The `HttpResponse` for this error.
/// - message: The message associated with this error. Defaults to `nil`.
/// - requestID: The request ID associated with this error. Defaults to `nil`.
/// - errorType: The error type associated with this error. Defaults to `nil`.
public init(
httpResponse: HttpResponse,
message: String? = nil,
requestID: String? = nil,
errorType: String? = nil
) {
self._errorType = errorType
self._statusCode = httpResponse.statusCode
self._headers = httpResponse.headers
self._requestID = requestID ?? httpResponse.headers.value(for: X_AMZN_REQUEST_ID_HEADER)
self._message = message
}
}

extension UnknownAWSHttpServiceError: WaiterTypedError {

/// The Smithy identifier, without namespace, for the type of this error, or `nil` if the
/// error has no known type.
public var waiterErrorType: String? { _errorType }
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public struct RestJSONError {
}

/// Filter additional information from error name and sanitize it
// Reference: https://awslabs.github.io/smithy/1.0/spec/aws/aws-restjson1-protocol.html#operation-error-serialization
/// Reference: https://awslabs.github.io/smithy/1.0/spec/aws/aws-restjson1-protocol.html#operation-error-serialization
static func sanitizeErrorType(_ type: String?) -> String? {
guard let errorType = type else {
return type
Expand Down
6 changes: 4 additions & 2 deletions codegen/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ appendLibTarget(name: "aws_restjson", path: "\(baseDirLocal)/aws-restjson")
appendTstTarget(name: "aws_restjsonTests", path: "\(baseDirLocal)/aws-restjson", dependency: "aws_restjson")
appendLibTarget(name: "rest_json_extras", path: "\(baseDirLocal)/rest_json_extras")
appendTstTarget(name: "rest_json_extrasTests", path: "\(baseDirLocal)/rest_json_extras", dependency: "rest_json_extras")
appendLibTarget(name: "Waiters", path: "\(baseDirLocal)/Waiters")
appendTstTarget(name: "WaitersTests", path: "./protocol-test-codegen-local/Tests", dependency: "Waiters")

func appendLibTarget(name: String, path: String) {
package.targets.append(
Expand Down Expand Up @@ -103,7 +105,7 @@ if let smithySwiftDir = ProcessInfo.processInfo.environment["SMITHY_SWIFT_CI_DIR
]
} else {
package.dependencies += [
.package(path: "~/Projects/Amplify/SwiftSDK/smithy-swift"),
.package(path: "~/Projects/Amplify/SwiftSDK/aws-sdk-swift"),
.package(path: "../../smithy-swift"),
.package(path: "../../aws-sdk-swift"),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import XCTest
@testable import Waiters
@testable import ClientRuntime

class ErrorTypeMatcherTests: XCTestCase {

// expected errorType for these tests: "MyError"

// MARK: - errorType matcher

func test_errorType_matchesWhenErrorTypeMatchesAndErrorIsAWaiterTypedError() async throws {
let error = WaiterTypedErrorThatMatches()
let subject = try WaitersClient.errorTypeMatcherWaiterConfig().acceptors[0]
let match = subject.evaluate(input: anInput, result: .failure(error))
XCTAssertEqual(match, .success(.failure(error)))
}

func test_errorType_doesNotMatchWhenErrorTypeDoesNotMatchAndErrorIsAWaiterTypedError() async throws {
let error = WaiterTypedErrorThatDoesntMatch()
let subject = try WaitersClient.errorTypeMatcherWaiterConfig().acceptors[0]
let match = subject.evaluate(input: anInput, result: .failure(error))
XCTAssertNil(match)
}

func test_errorType_doesNotMatchWhenErrorTypeMatchesButErrorIsNotAWaiterTypedError() async throws {
let error = NotAWaiterTypedError()
let subject = try WaitersClient.errorTypeMatcherWaiterConfig().acceptors[0]
let match = subject.evaluate(input: anInput, result: .failure(error))
XCTAssertNil(match)
}

func test_errorType_doesNotMatchWhenResultIsSuccess() async throws {
let response = GetWidgetOutputResponse()
let subject = try WaitersClient.errorTypeMatcherWaiterConfig().acceptors[0]
let match = subject.evaluate(input: anInput, result: .success(response))
XCTAssertNil(match)
}
}

// Error types used in tests above

private struct WaiterTypedErrorThatMatches: WaiterTypedError, Equatable {

var waiterErrorType: String? { "MyError" }
}

private struct WaiterTypedErrorThatDoesntMatch: WaiterTypedError, Equatable {

var waiterErrorType: String? { "OtherError" }
}

private struct NotAWaiterTypedError: Error, Equatable { // An error but not a WaiterTypedError

var waiterErrorType: String? { "MyError" }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import XCTest
@testable import Waiters
@testable import ClientRuntime

class InputOutputMatcherTests: XCTestCase {

// JMESPath expression: input.stringProperty == output.stringProperty
// JMESPath comparator: booleanEquals
// JMESPath expected value: true

// inputOutput tests are just on the input & output properties, because all the other logic
// in them is shared with output matchers, which are tested more comprehensively.

func test_inputOutput_acceptorMatchesWhenInputAndOutputPropertiesMatch() async throws {
let value = UUID().uuidString
let input = GetWidgetInput(stringProperty: value)
let output = GetWidgetOutputResponse(stringProperty: value)
let subject = try WaitersClient.inputOutputPropertyMatcherWaiterConfig().acceptors[0]
let match = subject.evaluate(input: input, result: .success(output))
XCTAssertEqual(match, .success(.success(output)))
}

func test_inputOutput_acceptorFailsToMatchWhenInputAndOutputPropertiesDontMatch() async throws {
let value = UUID().uuidString
let input = GetWidgetInput(stringProperty: value)
let output = GetWidgetOutputResponse(stringProperty: value + "xxx")
let subject = try WaitersClient.inputOutputPropertyMatcherWaiterConfig().acceptors[0]
let match = subject.evaluate(input: input, result: .success(output))
XCTAssertNil(match)
}
}

Loading