Skip to content

Commit 008c542

Browse files
committed
DRY: move the error handling to the _run() function
1 parent cd00948 commit 008c542

File tree

2 files changed

+15
-28
lines changed

2 files changed

+15
-28
lines changed

Sources/AWSLambdaRuntime/LambdaRuntime+ServiceLifecycle.swift

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,7 @@ import ServiceLifecycle
1818
extension LambdaRuntime: Service {
1919
public func run() async throws {
2020
try await cancelWhenGracefulShutdown {
21-
do {
22-
try await self._run()
23-
} catch {
24-
// catch top level errors that have not been handled until now
25-
// this avoids the runtime to crash and generate a backtrace
26-
self.logger.error("LambdaRuntime.run() failed with error", metadata: ["error": "\(error)"])
27-
if let error = error as? LambdaRuntimeError,
28-
error.code != .connectionToControlPlaneLost
29-
{
30-
// if the error is a LambdaRuntimeError but not a connection error,
31-
// we rethrow it to preserve existing behaviour
32-
throw error
33-
}
34-
}
21+
try await self._run()
3522
}
3623
}
3724
}

Sources/AWSLambdaRuntime/LambdaRuntime.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,7 @@ public final class LambdaRuntime<Handler>: Sendable where Handler: StreamingLamb
5959
#if !ServiceLifecycleSupport
6060
@inlinable
6161
internal func run() async throws {
62-
do {
63-
try await _run()
64-
} catch {
65-
// catch top level errors that have not been handled until now
66-
// this avoids the runtime to crash and generate a backtrace
67-
self.logger.error("LambdaRuntime.run() failed with error", metadata: ["error": "\(error)"])
68-
if let error = error as? LambdaRuntimeError,
69-
error.code != .connectionToControlPlaneLost
70-
{
71-
// if the error is a LambdaRuntimeError but not a connection error,
72-
// we rethrow it to preserve existing behaviour
73-
throw error
74-
}
75-
}
62+
try await _run()
7663
}
7764
#endif
7865

@@ -107,6 +94,7 @@ public final class LambdaRuntime<Handler>: Sendable where Handler: StreamingLamb
10794
let ip = String(ipAndPort[0])
10895
guard let port = Int(ipAndPort[1]) else { throw LambdaRuntimeError(code: .invalidPort) }
10996

97+
do {
11098
try await LambdaRuntimeClient.withRuntimeClient(
11199
configuration: .init(ip: ip, port: port),
112100
eventLoop: self.eventLoop,
@@ -118,6 +106,18 @@ public final class LambdaRuntime<Handler>: Sendable where Handler: StreamingLamb
118106
logger: self.logger
119107
)
120108
}
109+
} catch {
110+
// catch top level errors that have not been handled until now
111+
// this avoids the runtime to crash and generate a backtrace
112+
self.logger.error("LambdaRuntime.run() failed with error", metadata: ["error": "\(error)"])
113+
if let error = error as? LambdaRuntimeError,
114+
error.code != .connectionToControlPlaneLost
115+
{
116+
// if the error is a LambdaRuntimeError but not a connection error,
117+
// we rethrow it to preserve existing behaviour
118+
throw error
119+
}
120+
}
121121

122122
} else {
123123

0 commit comments

Comments
 (0)