Skip to content

Commit 2a89f6e

Browse files
committed
give better error when lambda fails to start
motivation: in error cases where lamnda fails to start the process fails silently and exists with code 0 changes: change the blocking API to check on the result and fatalError if lambda cannot be started / run
1 parent 2067f4a commit 2a89f6e

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ extension Lambda {
3535
/// - body: Code to run within the context of the mock server. Typically this would be a Lambda.run function call.
3636
///
3737
/// - note: This API is designed stricly for local testing and is behind a DEBUG flag
38-
@discardableResult
39-
static func withLocalServer<Value>(invocationEndpoint: String? = nil, _ body: @escaping () -> Value) throws -> Value {
38+
internal static func withLocalServer<Value>(invocationEndpoint: String? = nil, _ body: @escaping () -> Value) throws -> Value {
4039
let server = LocalLambda.Server(invocationEndpoint: invocationEndpoint)
4140
try server.start().wait()
42-
defer { try! server.stop() } // FIXME:
41+
defer { try! server.stop() }
4342
return body()
4443
}
4544
}

Sources/AWSLambdaRuntimeCore/Lambda+String.swift

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ extension Lambda {
2525
///
2626
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
2727
public static func run(_ closure: @escaping StringClosure) {
28-
self.run(closure: closure)
28+
if case .failure(let error) = self.run(closure: closure) {
29+
fatalError("\(error)")
30+
}
2931
}
3032

3133
/// An asynchronous Lambda Closure that takes a `String` and returns a `Result<Void, Error>` via a completion handler.
@@ -38,17 +40,17 @@ extension Lambda {
3840
///
3941
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
4042
public static func run(_ closure: @escaping StringVoidClosure) {
41-
self.run(closure: closure)
43+
if case .failure(let error) = self.run(closure: closure) {
44+
fatalError("\(error)")
45+
}
4246
}
4347

4448
// for testing
45-
@discardableResult
4649
internal static func run(configuration: Configuration = .init(), closure: @escaping StringClosure) -> Result<Int, Error> {
4750
self.run(configuration: configuration, handler: StringClosureWrapper(closure))
4851
}
4952

5053
// for testing
51-
@discardableResult
5254
internal static func run(configuration: Configuration = .init(), closure: @escaping StringVoidClosure) -> Result<Int, Error> {
5355
self.run(configuration: configuration, handler: StringVoidClosureWrapper(closure))
5456
}

Sources/AWSLambdaRuntimeCore/Lambda.swift

+9-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public enum Lambda {
3737
///
3838
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
3939
public static func run(_ handler: Handler) {
40-
self.run(handler: handler)
40+
if case .failure(let error) = self.run(handler: handler) {
41+
fatalError("\(error)")
42+
}
4143
}
4244

4345
/// Run a Lambda defined by implementing the `LambdaHandler` protocol provided via a `LambdaHandlerFactory`.
@@ -49,7 +51,9 @@ public enum Lambda {
4951
///
5052
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
5153
public static func run(_ factory: @escaping HandlerFactory) {
52-
self.run(factory: factory)
54+
if case .failure(let error) = self.run(factory: factory) {
55+
fatalError("\(error)")
56+
}
5357
}
5458

5559
/// Run a Lambda defined by implementing the `LambdaHandler` protocol provided via a factory, typically a constructor.
@@ -59,7 +63,9 @@ public enum Lambda {
5963
///
6064
/// - note: This is a blocking operation that will run forever, as its lifecycle is managed by the AWS Lambda Runtime Engine.
6165
public static func run(_ factory: @escaping (InitializationContext) throws -> Handler) {
62-
self.run(factory: factory)
66+
if case .failure(let error) = self.run(factory: factory) {
67+
fatalError("\(error)")
68+
}
6369
}
6470

6571
/// Utility to access/read environment variables
@@ -71,13 +77,11 @@ public enum Lambda {
7177
}
7278

7379
// for testing and internal use
74-
@discardableResult
7580
internal static func run(configuration: Configuration = .init(), handler: Handler) -> Result<Int, Error> {
7681
self.run(configuration: configuration, factory: { $0.eventLoop.makeSucceededFuture(handler) })
7782
}
7883

7984
// for testing and internal use
80-
@discardableResult
8185
internal static func run(configuration: Configuration = .init(), factory: @escaping (InitializationContext) throws -> Handler) -> Result<Int, Error> {
8286
self.run(configuration: configuration, factory: { context -> EventLoopFuture<Handler> in
8387
let promise = context.eventLoop.makePromise(of: Handler.self)
@@ -95,7 +99,6 @@ public enum Lambda {
9599
}
96100

97101
// for testing and internal use
98-
@discardableResult
99102
internal static func run(configuration: Configuration = .init(), factory: @escaping HandlerFactory) -> Result<Int, Error> {
100103
let _run = { (configuration: Configuration, factory: @escaping HandlerFactory) -> Result<Int, Error> in
101104
Backtrace.install()

0 commit comments

Comments
 (0)