Files
swift-aws-lambda-runtime/Examples/ErrorHandling/Lambda.swift
T
c915322eca API Refactoring (#273)
motivation: define stable API in preperation 1.0 release

changes:
* require swift 5.7, remove redundant backwards compatibility code
* make LambdaHandler, EventLoopLambdaHandler, and ByteBufferLambdaHandler disjointed protocols to reduce API surface area
* create coding wrappers for LambdaHandler and EventLoopLambdaHandler to provide bridge to ByteBufferLambdaHandler
* reuse output ByteBuffer to reduce allocations
* add new SimpleLambdaHandler with no-op initializer for simple lambda use cases
* update callsites and tests
* update examples

Co-authored-by: Yim Lee <yim_lee@apple.com>
Co-authored-by: Fabian Fett <fabianfett@apple.com>
2022-11-09 10:08:36 -08:00

105 lines
3.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2020 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 AWSLambdaRuntime
// MARK: - Run Lambda
@main
struct MyLambda: SimpleLambdaHandler {
func handle(_ request: Request, context: LambdaContext) async throws -> Response {
// switch over the error type "requested" by the request, and trigger such error accordingly
switch request.error {
// no error here!
case .none:
return Response(awsRequestID: context.requestID, requestID: request.requestID, status: .ok)
// trigger a "managed" error - domain specific business logic failure
case .managed:
return Response(awsRequestID: context.requestID, requestID: request.requestID, status: .error)
// trigger an "unmanaged" error - an unexpected Swift Error triggered while processing the request
case .unmanaged(let error):
throw UnmanagedError(description: error)
// trigger a "fatal" error - a panic type error which will crash the process
case .fatal:
fatalError("crash!")
}
}
}
// MARK: - Request and Response
struct Request: Codable {
let requestID: String
let error: Error
public init(requestID: String, error: Error? = nil) {
self.requestID = requestID
self.error = error ?? .none
}
public enum Error: Codable, RawRepresentable {
case none
case managed
case unmanaged(String)
case fatal
public init?(rawValue: String) {
switch rawValue {
case "none":
self = .none
case "managed":
self = .managed
case "fatal":
self = .fatal
default:
self = .unmanaged(rawValue)
}
}
public var rawValue: String {
switch self {
case .none:
return "none"
case .managed:
return "managed"
case .fatal:
return "fatal"
case .unmanaged(let error):
return error
}
}
}
}
struct Response: Codable {
let awsRequestID: String
let requestID: String
let status: Status
public init(awsRequestID: String, requestID: String, status: Status) {
self.awsRequestID = awsRequestID
self.requestID = requestID
self.status = status
}
public enum Status: Int, Codable {
case ok
case error
}
}
struct UnmanagedError: Error {
let description: String
}