-
Notifications
You must be signed in to change notification settings - Fork 102
/
Lambda.swift
104 lines (91 loc) · 2.97 KB
/
Lambda.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//===----------------------------------------------------------------------===//
//
// 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
}