Skip to content

Commit 9a852d9

Browse files
committed
style(TaskOperation): add separate error for retrieving result without starting
1 parent 571419d commit 9a852d9

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

Sources/AsyncObjects/TaskOperation.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public final class TaskOperation<R: Sendable>: Operation, AsyncObject,
9393
/// Will be success if provided operation completed successfully,
9494
/// or failure returned with error.
9595
public var result: Result<R, Error> {
96-
get async { (await execTask?.result) ?? .failure(CancellationError()) }
96+
get async { (await execTask?.result) ?? .failure(EarlyInvokeError()) }
9797
}
9898

9999
/// Creates a new operation that executes the provided asynchronous task.
@@ -268,3 +268,11 @@ public final class TaskOperation<R: Sendable>: Operation, AsyncObject,
268268
try? await _withPromisedContinuation()
269269
}
270270
}
271+
272+
/// An error that indicates that operation result
273+
/// requested without starting operation.
274+
///
275+
/// Error is thrown by ``TaskOperation/result``
276+
/// if the operation hasn't been started yet with either
277+
/// ``TaskOperation/start()`` or ``TaskOperation/signal()``.
278+
public struct EarlyInvokeError: Error, Sendable {}

Tests/AsyncObjectsTests/TaskOperationTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,18 @@ class TaskOperationTests: XCTestCase {
208208
await operation.wait()
209209
}
210210
}
211+
212+
func testNotStartedError() async throws {
213+
let operation = TaskOperation { try await Self.sleep(seconds: 1) }
214+
let result = await operation.result
215+
switch result {
216+
case .success: XCTFail("Unexpected operation result")
217+
case .failure(let error):
218+
XCTAssertTrue(type(of: error) == EarlyInvokeError.self)
219+
print(
220+
"[\(#function)] [\(type(of: error))] \(error.localizedDescription)"
221+
)
222+
XCTAssertFalse(error.localizedDescription.isEmpty)
223+
}
224+
}
211225
}

0 commit comments

Comments
 (0)