Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ internal final class LambdaChannelHandler<Delegate: LambdaChannelHandlerDelegate
self.reusableErrorBuffer!.clear()
}

let errorResponse = ErrorResponse(errorType: Consts.functionError, errorMessage: "\(error)")
let errorResponse = ErrorResponse(errorType: "\(type(of: error))", errorMessage: "\(error)")
// TODO: Write this directly into our ByteBuffer
let bytes = errorResponse.toJSONBytes()
self.reusableErrorBuffer!.writeBytes(bytes)
Expand Down
1 change: 0 additions & 1 deletion Sources/AWSLambdaRuntime/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ enum Consts {
static let postResponseURLSuffix = "/response"
static let postErrorURLSuffix = "/error"
static let postInitErrorURL = "\(apiPrefix)/runtime/init/error"
static let functionError = "FunctionError"
static let initializationError = "InitializationError"
}

Expand Down
74 changes: 74 additions & 0 deletions Tests/AWSLambdaRuntimeTests/LambdaRuntimeClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,78 @@ struct LambdaRuntimeClientTests {
}
}
}

@Test(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great !

"reportError() sends the correct errorType for different Error types"
)
@available(LambdaSwift 2.0, *)
func testReportErrorReturnsProperErrorType() async throws {
// Custom error types for testing
struct MyCustomError: Error {
let message: String
}

enum MyEnumError: Error {
case anotherCase(String)
}

struct ErrorReportingBehavior: LambdaServerBehavior {
let requestId = UUID().uuidString
let event = "error-testing"
let expectedErrorType: String

func getInvocation() -> GetInvocationResult {
.success((self.requestId, self.event))
}

func processResponse(requestId: String, response: String?) -> Result<String?, ProcessResponseError> {
Issue.record("should not process response, expecting error report")
return .failure(.internalServerError)
}

func processError(requestId: String, error: ErrorResponse) -> Result<Void, ProcessErrorError> {
#expect(self.requestId == requestId)
#expect(
error.errorType == self.expectedErrorType,
"Expected errorType '\(self.expectedErrorType)' but got '\(error.errorType)'"
)
return .success(())
}

func processInitError(error: ErrorResponse) -> Result<Void, ProcessErrorError> {
Issue.record("should not report init error")
return .failure(.internalServerError)
}
}

// Test with MyCustomError
try await withMockServer(behaviour: ErrorReportingBehavior(expectedErrorType: "MyCustomError")) { port in
let configuration = LambdaRuntimeClient.Configuration(ip: "127.0.0.1", port: port)

try await LambdaRuntimeClient.withRuntimeClient(
configuration: configuration,
eventLoop: NIOSingletons.posixEventLoopGroup.next(),
logger: self.logger
) { runtimeClient in
let (_, writer) = try await runtimeClient.nextInvocation()
let error = MyCustomError(message: "Something went wrong")
try await writer.reportError(error)
}
}

// Test with MyEnumError
try await withMockServer(behaviour: ErrorReportingBehavior(expectedErrorType: "MyEnumError")) { port in
let configuration = LambdaRuntimeClient.Configuration(ip: "127.0.0.1", port: port)

try await LambdaRuntimeClient.withRuntimeClient(
configuration: configuration,
eventLoop: NIOSingletons.posixEventLoopGroup.next(),
logger: self.logger
) { runtimeClient in
let (_, writer) = try await runtimeClient.nextInvocation()
let error = MyEnumError.anotherCase("test")
try await writer.reportError(error)
}
}
}
}