Skip to content

Commit a12974f

Browse files
committed
Capability to throw errorType besides FunctionError
1 parent c4f380e commit a12974f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// LambdaError.swift
3+
//
4+
//
5+
// Created by Ben Rosen on 1/6/24.
6+
//
7+
8+
import Foundation
9+
10+
public struct LambdaError: Error {
11+
let errorType: String
12+
let errorMessage: String
13+
14+
public init(errorType: String, errorMessage: String) {
15+
self.errorType = errorType
16+
self.errorMessage = errorMessage
17+
}
18+
}

Sources/AWSLambdaRuntimeCore/LambdaRuntimeClient.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@ internal struct LambdaRuntimeClient {
6969
headers = LambdaRuntimeClient.defaultHeaders
7070
case .failure(let error):
7171
url += Consts.postErrorURLSuffix
72-
let errorResponse = ErrorResponse(errorType: Consts.functionError, errorMessage: "\(error)")
72+
73+
/*
74+
If a LambdaError is thrown, we allow a custom errorType to be thrown from the function
75+
*/
76+
var errorResponse: ErrorResponse
77+
if let lambdaError = error as? LambdaError {
78+
errorResponse = ErrorResponse(errorType: lambdaError.errorType, errorMessage: lambdaError.errorMessage)
79+
} else {
80+
errorResponse = ErrorResponse(errorType: Consts.functionError, errorMessage: "\(error)")
81+
}
82+
7383
let bytes = errorResponse.toJSONBytes()
7484
body = self.allocator.buffer(capacity: bytes.count)
7585
body!.writeBytes(bytes)

0 commit comments

Comments
 (0)