Skip to content

Commit 2ba6da4

Browse files
authored
feat: Smithy Waiters (#755)
* feat: Add WaiterTypedError compliance to UnknownAWSHttpServiceError (#754) * feat: Test code-generated Waiters components (#732) * fix: Add waiters generated test for AND and for number equality & inequality (#762)
1 parent b605643 commit 2ba6da4

File tree

14 files changed

+879
-7
lines changed

14 files changed

+879
-7
lines changed

AWSClientRuntime/Sources/Errors/UnknownAWSHttpServiceError.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import ClientRuntime
77

88
/// AWS specific Service Error structure used when exact error could not be deduced from the `HttpResponse`
99
public struct UnknownAWSHttpServiceError: AWSHttpServiceError, Equatable {
10+
/// The error type for this error, or `nil` if the type is not known.
11+
public var _errorType: String?
12+
1013
public var _isThrottling: Bool = false
1114

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

2528
extension UnknownAWSHttpServiceError {
26-
public init(httpResponse: HttpResponse, message: String? = nil, requestID: String? = nil) {
29+
30+
/// Creates an `UnknownAWSHttpServiceError` from a `HttpResponse` and associated parameters.
31+
/// - Parameters:
32+
/// - httpResponse: The `HttpResponse` for this error.
33+
/// - message: The message associated with this error. Defaults to `nil`.
34+
/// - requestID: The request ID associated with this error. Defaults to `nil`.
35+
/// - errorType: The error type associated with this error. Defaults to `nil`.
36+
public init(
37+
httpResponse: HttpResponse,
38+
message: String? = nil,
39+
requestID: String? = nil,
40+
errorType: String? = nil
41+
) {
42+
self._errorType = errorType
2743
self._statusCode = httpResponse.statusCode
2844
self._headers = httpResponse.headers
2945
self._requestID = requestID ?? httpResponse.headers.value(for: X_AMZN_REQUEST_ID_HEADER)
3046
self._message = message
3147
}
3248
}
49+
50+
extension UnknownAWSHttpServiceError: WaiterTypedError {
51+
52+
/// The Smithy identifier, without namespace, for the type of this error, or `nil` if the
53+
/// error has no known type.
54+
public var waiterErrorType: String? { _errorType }
55+
}

AWSClientRuntime/Sources/Protocols/RestJSON/RestJSONError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public struct RestJSONError {
5050
}
5151

5252
/// Filter additional information from error name and sanitize it
53-
// Reference: https://awslabs.github.io/smithy/1.0/spec/aws/aws-restjson1-protocol.html#operation-error-serialization
53+
/// Reference: https://awslabs.github.io/smithy/1.0/spec/aws/aws-restjson1-protocol.html#operation-error-serialization
5454
static func sanitizeErrorType(_ type: String?) -> String? {
5555
guard let errorType = type else {
5656
return type

codegen/Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ appendLibTarget(name: "aws_restjson", path: "\(baseDirLocal)/aws-restjson")
5959
appendTstTarget(name: "aws_restjsonTests", path: "\(baseDirLocal)/aws-restjson", dependency: "aws_restjson")
6060
appendLibTarget(name: "rest_json_extras", path: "\(baseDirLocal)/rest_json_extras")
6161
appendTstTarget(name: "rest_json_extrasTests", path: "\(baseDirLocal)/rest_json_extras", dependency: "rest_json_extras")
62+
appendLibTarget(name: "Waiters", path: "\(baseDirLocal)/Waiters")
63+
appendTstTarget(name: "WaitersTests", path: "./protocol-test-codegen-local/Tests", dependency: "Waiters")
6264

6365
func appendLibTarget(name: String, path: String) {
6466
package.targets.append(
@@ -103,7 +105,7 @@ if let smithySwiftDir = ProcessInfo.processInfo.environment["SMITHY_SWIFT_CI_DIR
103105
]
104106
} else {
105107
package.dependencies += [
106-
.package(path: "~/Projects/Amplify/SwiftSDK/smithy-swift"),
107-
.package(path: "~/Projects/Amplify/SwiftSDK/aws-sdk-swift"),
108+
.package(path: "../../smithy-swift"),
109+
.package(path: "../../aws-sdk-swift"),
108110
]
109111
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
@testable import Waiters
10+
@testable import ClientRuntime
11+
12+
class ErrorTypeMatcherTests: XCTestCase {
13+
14+
// expected errorType for these tests: "MyError"
15+
16+
// MARK: - errorType matcher
17+
18+
func test_errorType_matchesWhenErrorTypeMatchesAndErrorIsAWaiterTypedError() async throws {
19+
let error = WaiterTypedErrorThatMatches()
20+
let subject = try WaitersClient.errorTypeMatcherWaiterConfig().acceptors[0]
21+
let match = subject.evaluate(input: anInput, result: .failure(error))
22+
XCTAssertEqual(match, .success(.failure(error)))
23+
}
24+
25+
func test_errorType_doesNotMatchWhenErrorTypeDoesNotMatchAndErrorIsAWaiterTypedError() async throws {
26+
let error = WaiterTypedErrorThatDoesntMatch()
27+
let subject = try WaitersClient.errorTypeMatcherWaiterConfig().acceptors[0]
28+
let match = subject.evaluate(input: anInput, result: .failure(error))
29+
XCTAssertNil(match)
30+
}
31+
32+
func test_errorType_doesNotMatchWhenErrorTypeMatchesButErrorIsNotAWaiterTypedError() async throws {
33+
let error = NotAWaiterTypedError()
34+
let subject = try WaitersClient.errorTypeMatcherWaiterConfig().acceptors[0]
35+
let match = subject.evaluate(input: anInput, result: .failure(error))
36+
XCTAssertNil(match)
37+
}
38+
39+
func test_errorType_doesNotMatchWhenResultIsSuccess() async throws {
40+
let response = GetWidgetOutputResponse()
41+
let subject = try WaitersClient.errorTypeMatcherWaiterConfig().acceptors[0]
42+
let match = subject.evaluate(input: anInput, result: .success(response))
43+
XCTAssertNil(match)
44+
}
45+
}
46+
47+
// Error types used in tests above
48+
49+
private struct WaiterTypedErrorThatMatches: WaiterTypedError, Equatable {
50+
51+
var waiterErrorType: String? { "MyError" }
52+
}
53+
54+
private struct WaiterTypedErrorThatDoesntMatch: WaiterTypedError, Equatable {
55+
56+
var waiterErrorType: String? { "OtherError" }
57+
}
58+
59+
private struct NotAWaiterTypedError: Error, Equatable { // An error but not a WaiterTypedError
60+
61+
var waiterErrorType: String? { "MyError" }
62+
}
63+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
@testable import Waiters
10+
@testable import ClientRuntime
11+
12+
class InputOutputMatcherTests: XCTestCase {
13+
14+
// JMESPath expression: input.stringProperty == output.stringProperty
15+
// JMESPath comparator: booleanEquals
16+
// JMESPath expected value: true
17+
18+
// inputOutput tests are just on the input & output properties, because all the other logic
19+
// in them is shared with output matchers, which are tested more comprehensively.
20+
21+
func test_inputOutput_acceptorMatchesWhenInputAndOutputPropertiesMatch() async throws {
22+
let value = UUID().uuidString
23+
let input = GetWidgetInput(stringProperty: value)
24+
let output = GetWidgetOutputResponse(stringProperty: value)
25+
let subject = try WaitersClient.inputOutputPropertyMatcherWaiterConfig().acceptors[0]
26+
let match = subject.evaluate(input: input, result: .success(output))
27+
XCTAssertEqual(match, .success(.success(output)))
28+
}
29+
30+
func test_inputOutput_acceptorFailsToMatchWhenInputAndOutputPropertiesDontMatch() async throws {
31+
let value = UUID().uuidString
32+
let input = GetWidgetInput(stringProperty: value)
33+
let output = GetWidgetOutputResponse(stringProperty: value + "xxx")
34+
let subject = try WaitersClient.inputOutputPropertyMatcherWaiterConfig().acceptors[0]
35+
let match = subject.evaluate(input: input, result: .success(output))
36+
XCTAssertNil(match)
37+
}
38+
}
39+

0 commit comments

Comments
 (0)