mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-06-02 07:27:33 +00:00
### Motivation: Fix for Issue [#580](https://github.com/swift-server/swift-aws-lambda-runtime/issues/580), by making it so that the `errorType` in failed requests will be the type of the error entity, rather than a hardcoded string of `FunctionError`. This allows orchestration within step functions that perform retry/catch logic based on different error output types. ### Modifications: At a high level, the issue is that swift-aws-lambda-runtime, when an error is thrown, outputs the errorType as hardcoded to FunctionError. You can see that [here](https://github.com/swift-server/swift-aws-lambda-runtime/blob/main/Sources/AWSLambdaRuntime/LambdaRuntimeClient%2BChannelHandler.swift#L337): ``` let errorResponse = ErrorResponse(errorType: Consts.functionError, errorMessage: "\(error)") ``` This PR changes this for all cases to output the type of the error, rather than the hardcoded string: ``` let errorResponse = ErrorResponse(errorType: "\(type(of: error))", errorMessage: "\(error)") ``` Now, I will show 2 examples with this solution: ``` let runtime = LambdaRuntime { (event: Input, context: LambdaContext) in enum MyTestErrorType: Error { case testError } throw MyTestErrorType.testError } // outputs {"errorType":"MyTestErrorType","errorMessage":"testError"} ``` ``` let dynamoDB: DynamoDB = DynamoDB(client: .init()) let runtime = LambdaRuntime { (event: Input, context: LambdaContext) in let _ = try await dynamoDB.putItem(DynamoDB.PutItemInput(item: [:], tableName: "")) return Output() } // outputs {"errorType":"AWSClientError","errorMessage":"ValidationError: Length of PutItemInput.tableName (0) is less than minimum allowed value 1."} ```
132 lines
5.3 KiB
Swift
132 lines
5.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftAWSLambdaRuntime open source project
|
|
//
|
|
// Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIOConcurrencyHelpers
|
|
import NIOPosix
|
|
|
|
// import Synchronization
|
|
|
|
enum Consts {
|
|
static let apiPrefix = "/2018-06-01"
|
|
static let invocationURLPrefix = "\(apiPrefix)/runtime/invocation"
|
|
static let getNextInvocationURLSuffix = "/next"
|
|
static let postResponseURLSuffix = "/response"
|
|
static let postErrorURLSuffix = "/error"
|
|
static let postInitErrorURL = "\(apiPrefix)/runtime/init/error"
|
|
static let initializationError = "InitializationError"
|
|
}
|
|
|
|
/// AWS Lambda HTTP Headers, used to populate the `LambdaContext` object.
|
|
enum AmazonHeaders {
|
|
static let requestID = "Lambda-Runtime-Aws-Request-Id"
|
|
static let traceID = "Lambda-Runtime-Trace-Id"
|
|
static let clientContext = "X-Amz-Client-Context"
|
|
static let cognitoIdentity = "X-Amz-Cognito-Identity"
|
|
static let deadline = "Lambda-Runtime-Deadline-Ms"
|
|
static let invokedFunctionARN = "Lambda-Runtime-Invoked-Function-Arn"
|
|
}
|
|
|
|
extension String {
|
|
func encodeAsJSONString(into bytes: inout [UInt8]) {
|
|
bytes.append(UInt8(ascii: "\""))
|
|
let stringBytes = self.utf8
|
|
var startCopyIndex = stringBytes.startIndex
|
|
var nextIndex = startCopyIndex
|
|
|
|
while nextIndex != stringBytes.endIndex {
|
|
switch stringBytes[nextIndex] {
|
|
case 0..<32, UInt8(ascii: "\""), UInt8(ascii: "\\"):
|
|
// All Unicode characters may be placed within the
|
|
// quotation marks, except for the characters that MUST be escaped:
|
|
// quotation mark, reverse solidus, and the control characters (U+0000
|
|
// through U+001F).
|
|
// https://tools.ietf.org/html/rfc7159#section-7
|
|
|
|
// copy the current range over
|
|
bytes.append(contentsOf: stringBytes[startCopyIndex..<nextIndex])
|
|
bytes.append(UInt8(ascii: "\\"))
|
|
bytes.append(stringBytes[nextIndex])
|
|
|
|
nextIndex = stringBytes.index(after: nextIndex)
|
|
startCopyIndex = nextIndex
|
|
default:
|
|
nextIndex = stringBytes.index(after: nextIndex)
|
|
}
|
|
}
|
|
|
|
// copy everything, that hasn't been copied yet
|
|
bytes.append(contentsOf: stringBytes[startCopyIndex..<nextIndex])
|
|
bytes.append(UInt8(ascii: "\""))
|
|
}
|
|
}
|
|
|
|
@available(LambdaSwift 2.0, *)
|
|
extension AmazonHeaders {
|
|
/// Generates (X-Ray) trace ID.
|
|
/// # Trace ID Format
|
|
/// A `trace_id` consists of three numbers separated by hyphens.
|
|
/// For example, `1-58406520-a006649127e371903a2de979`. This includes:
|
|
/// - The version number, that is, 1.
|
|
/// - The time of the original request, in Unix epoch time, in **8 hexadecimal digits**.
|
|
/// For example, 10:00AM December 1st, 2016 PST in epoch time is `1480615200` seconds, or `58406520` in hexadecimal digits.
|
|
/// - A 96-bit identifier for the trace, globally unique, in **24 hexadecimal digits**.
|
|
/// # References
|
|
/// - [Generating trace IDs](https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids)
|
|
/// - [Tracing header](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader)
|
|
static func generateXRayTraceID() -> String {
|
|
// The version number, that is, 1.
|
|
let version: UInt = 1
|
|
// The time of the original request, in Unix epoch time, in 8 hexadecimal digits.
|
|
let now = UInt32(LambdaClock().now.millisecondsSinceEpoch() / 1000)
|
|
let dateValue = String(now, radix: 16, uppercase: false)
|
|
let datePadding = String(repeating: "0", count: max(0, 8 - dateValue.count))
|
|
// A 96-bit identifier for the trace, globally unique, in 24 hexadecimal digits.
|
|
let identifier =
|
|
String(UInt64.random(in: UInt64.min...UInt64.max) | 1 << 63, radix: 16, uppercase: false)
|
|
+ String(UInt32.random(in: UInt32.min...UInt32.max) | 1 << 31, radix: 16, uppercase: false)
|
|
return "\(version)-\(datePadding)\(dateValue)-\(identifier)"
|
|
}
|
|
}
|
|
|
|
/// Temporary storage for value being sent from one isolation domain to another
|
|
// use NIOLockedValueBox instead of Mutex to avoid compiler crashes on 6.0
|
|
// see https://github.com/swiftlang/swift/issues/78048
|
|
@usableFromInline
|
|
struct SendingStorage<Value>: ~Copyable, @unchecked Sendable {
|
|
@usableFromInline
|
|
struct ValueAlreadySentError: Error {
|
|
@usableFromInline
|
|
init() {}
|
|
}
|
|
|
|
@usableFromInline
|
|
// let storage: Mutex<Value?>
|
|
let storage: NIOLockedValueBox<Value?>
|
|
|
|
@inlinable
|
|
init(_ value: sending Value) {
|
|
self.storage = .init(value)
|
|
}
|
|
|
|
@inlinable
|
|
func get() throws -> Value {
|
|
// try self.storage.withLock {
|
|
try self.storage.withLockedValue {
|
|
guard let value = $0 else { throw ValueAlreadySentError() }
|
|
$0 = nil
|
|
return value
|
|
}
|
|
}
|
|
}
|