Skip to content

Commit b73914d

Browse files
committed
Add test for allowed but unregistered error type
1 parent 046cd44 commit b73914d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Sources/DistributedActors/ClusterSystem.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,10 +1502,10 @@ public struct ClusterInvocationResultHandler: DistributedTargetInvocationResultH
15021502
case .all: // compiler gets confused if this is grouped together with above
15031503
reply = .init(callID: callID, error: codableError)
15041504
default:
1505-
reply = .init(callID: callID, error: GenericRemoteCallError(message: "Remote call error of [\(errorType)] type occurred"))
1505+
reply = .init(callID: callID, error: GenericRemoteCallError(errorType: errorType))
15061506
}
15071507
} else {
1508-
reply = .init(callID: callID, error: GenericRemoteCallError(message: "Remote call error of [\(errorType)] type occurred"))
1508+
reply = .init(callID: callID, error: GenericRemoteCallError(errorType: errorType))
15091509
}
15101510
try await channel.writeAndFlush(TransportEnvelope(envelope: Payload(payload: .message(reply)), recipient: recipient))
15111511
}
@@ -1595,6 +1595,14 @@ struct RemoteCallReply<Value: Codable>: AnyRemoteCallReply {
15951595

15961596
public struct GenericRemoteCallError: Error, Codable {
15971597
public let message: String
1598+
1599+
init(message: String) {
1600+
self.message = message
1601+
}
1602+
1603+
init(errorType: Any.Type) {
1604+
self.message = "Remote call error of [\(errorType)] type occurred"
1605+
}
15981606
}
15991607

16001608
public enum ClusterSystemError: DistributedActorSystemError {

Tests/DistributedActorsTests/RemoteCallTests.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,10 @@ final class RemoteCallTests: ClusteredActorSystemsXCTestCase {
231231
let error = try await shouldThrow {
232232
_ = try await remoteGreeterRef.helloThrow(codable: true)
233233
}
234-
guard error is GenericRemoteCallError else {
234+
guard let remoteCallError = error as? GenericRemoteCallError else {
235235
throw TestError("Expected GenericRemoteCallError, got \(error)")
236236
}
237+
remoteCallError.message.shouldStartWith(prefix: "Remote call error of [GreeterCodableError] type occurred")
237238
}
238239

239240
func test_remoteCall_customCodableErrorAllowList_errorInList() async throws {
@@ -260,6 +261,28 @@ final class RemoteCallTests: ClusteredActorSystemsXCTestCase {
260261
}
261262
}
262263

264+
func test_remoteCall_customCodableErrorAllowList_errorInListButNotRegistered() async throws {
265+
let local = await setUpNode("local") { settings in
266+
settings.remoteCall.codableErrorAllowance = .custom(allowedTypes: [GreeterCodableError.self, AnotherGreeterCodableError.self])
267+
}
268+
let remote = await setUpNode("remote") { settings in
269+
settings.remoteCall.codableErrorAllowance = .custom(allowedTypes: [GreeterCodableError.self, AnotherGreeterCodableError.self])
270+
}
271+
local.cluster.join(node: remote.cluster.uniqueNode)
272+
273+
let greeter = Greeter(actorSystem: local)
274+
let remoteGreeterRef = try Greeter.resolve(id: greeter.id, using: remote)
275+
276+
let error = try await shouldThrow {
277+
try await RemoteCall.with(timeout: .milliseconds(200)) {
278+
_ = try await remoteGreeterRef.helloThrow(codable: true)
279+
}
280+
}
281+
guard case RemoteCallError.timedOut = error else {
282+
throw TestError("Expected RemoteCallError.timedOut, got \(error)")
283+
}
284+
}
285+
263286
func test_remoteCall_customCodableErrorAllowList_errorNotInList() async throws {
264287
let local = await setUpNode("local") { settings in
265288
settings.remoteCall.codableErrorAllowance = .custom(allowedTypes: [AnotherGreeterCodableError.self])
@@ -279,9 +302,10 @@ final class RemoteCallTests: ClusteredActorSystemsXCTestCase {
279302
let error = try await shouldThrow {
280303
_ = try await remoteGreeterRef.helloThrow(codable: true)
281304
}
282-
guard error is GenericRemoteCallError else {
305+
guard let remoteCallError = error as? GenericRemoteCallError else {
283306
throw TestError("Expected GenericRemoteCallError, got \(error)")
284307
}
308+
remoteCallError.message.shouldStartWith(prefix: "Remote call error of [GreeterCodableError] type occurred")
285309
}
286310
}
287311

0 commit comments

Comments
 (0)