Skip to content

Commit 9646298

Browse files
committed
improve error reporting
1 parent cf37dd8 commit 9646298

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ extension DistributedActorSystem {
212212
/// some other mismatch between them happens. In general, this
213213
/// method is allowed to throw in any situation that might otherwise
214214
/// result in an illegal or unexpected invocation being performed.
215+
///
216+
/// Throws ``ExecuteDistributedTargetMissingAccessorError`` if the `target`
217+
/// does not resolve to a valid distributed function accessor, i.e. the
218+
/// call identifier is incorrect, corrupted, or simply not present in this process.
215219
public func executeDistributedTarget<Act>(
216220
on actor: Act,
217221
target: RemoteCallTarget,
@@ -574,10 +578,28 @@ public protocol DistributedActorSystemError: Error {}
574578

575579
@available(SwiftStdlib 5.7, *)
576580
public struct ExecuteDistributedTargetError: DistributedActorSystemError {
577-
let message: String
581+
public let errorCode: ErrorCode
582+
public let message: String
583+
584+
public enum ErrorCode {
585+
/// Thrown when unable to resolve the target identifier to a function accessor.
586+
/// This can happen when the identifier is corrupt, illegal, or wrong in the
587+
/// sense that the caller and callee do not have the called function recorded
588+
/// using the same identifier.
589+
case targetAccessorNotFound
590+
591+
/// A general issue during the execution of the distributed call target ocurred.
592+
case other
593+
}
578594

579595
public init(message: String) {
580596
self.message = message
597+
self.errorCode = .other
598+
}
599+
600+
public init(message: String, errorCode: ErrorCode) {
601+
self.message = message
602+
self.errorCode = errorCode
581603
}
582604
}
583605

stdlib/public/Distributed/DistributedMetadata.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,9 @@ func _getWitnessTablesFor(
112112
@available(SwiftStdlib 5.7, *)
113113
@_silgen_name("swift_distributed_makeDistributedTargetAccessorNotFoundError")
114114
internal // SPI Distributed
115-
func _makeDistributedTargetAccessorNotFoundError(
116-
_ targetNameStart: UnsafePointer<UInt8>,
117-
_ targetNameLength: UInt
118-
) -> Error {
119-
let name = String(decodingCString: targetNameStart, as: Unicode.UTF8.self)
120-
return ExecuteDistributedTargetError(message: "Could not find distributed accessor for target \(name)")
115+
func _makeDistributedTargetAccessorNotFoundError() -> Error {
116+
/// We don't include the name of the target in case the input was compromised.
117+
return ExecuteDistributedTargetError(
118+
message: "Failed to locate distributed function accessor",
119+
errorCode: .targetAccessorNotFound)
121120
}

test/Distributed/Runtime/distributed_actor_func_calls_remoteCall_bad_targets.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ func test() async throws {
5757
returning: String.self
5858
)
5959
} catch {
60-
// CHECK: << onThrow: ExecuteDistributedTargetError(message: "Could not find distributed accessor for target $s9BADMODULE7GreeterC5greet4nameS2S_tYaKFTE")
61-
// CHECK: << remoteCall throw: ExecuteDistributedTargetError(message: "Could not find distributed accessor for target $s9BADMODULE7GreeterC5greet4nameS2S_tYaKFTE")
60+
// CHECK: << onThrow: ExecuteDistributedTargetError(errorCode: Distributed.ExecuteDistributedTargetError.ErrorCode.targetAccessorNotFound, message: "Failed to locate distributed function accessor")
61+
// CHECK: << remoteCall throw: ExecuteDistributedTargetError(errorCode: Distributed.ExecuteDistributedTargetError.ErrorCode.targetAccessorNotFound, message: "Failed to locate distributed function accessor")
6262
print("caught error: \(error)")
63-
// CHECK: caught error: ExecuteDistributedTargetError(message: "Could not find distributed accessor for target $s9BADMODULE7GreeterC5greet4nameS2S_tYaKFTE")
63+
print("call target was: \(badTarget.identifier)")
64+
// CHECK: caught error: ExecuteDistributedTargetError(errorCode: Distributed.ExecuteDistributedTargetError.ErrorCode.targetAccessorNotFound, message: "Failed to locate distributed function accessor")
65+
// CHECK: call target was: $s9BADMODULE7GreeterC5greet4nameS2S_tYaKFTE
6466
}
6567
}
6668

test/Distributed/Runtime/distributed_actor_remoteCall.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ func test() async throws {
492492
invocationDecoder: &decodeErrDecoder,
493493
handler: FakeResultHandler()
494494
)
495-
// CHECK: ERROR: ExecuteDistributedTargetError(message: "Failed to decode of Int??? (for a test)")
495+
// CHECK: ERROR: ExecuteDistributedTargetError(errorCode: Distributed.ExecuteDistributedTargetError.ErrorCode.other, message: "Failed to decode of Int??? (for a test)")
496496

497497
print("done")
498498
// CHECK-NEXT: done

0 commit comments

Comments
 (0)