Skip to content

Commit 3d7e838

Browse files
Introduce status code into invalid channel state error
I didn't do this in 4494033 because the specification hadn't yet specified the status code (it was an outstanding question on the PR at time of implementing), but the newly-written spec [1] for other LiveMap getter methods _does_ specify the status code as being 400. So DRY up the creation of these errors, and supply a status code (assuming that the spec will be updated to specify 400 for these existing ones too). [1] ably/specification#341
1 parent 2a6b773 commit 3d7e838

7 files changed

Lines changed: 59 additions & 6 deletions

File tree

Sources/AblyLiveObjects/DefaultRealtimeObjects.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ internal final class DefaultRealtimeObjects: RealtimeObjects, LiveMapObjectPoolD
8282
// RTO1b: If the channel is in the DETACHED or FAILED state, the library should indicate an error with code 90001
8383
let currentChannelState = coreSDK.channelState
8484
if currentChannelState == .detached || currentChannelState == .failed {
85-
throw ARTErrorInfo.create(withCode: Int(ARTErrorCode.channelOperationFailedInvalidState.rawValue), message: "getRoot operation failed (invalid channel state: \(currentChannelState))")
85+
throw LiveObjectsError.objectsOperationFailedInvalidChannelState(
86+
operationDescription: "getRoot",
87+
channelState: currentChannelState,
88+
)
89+
.toARTErrorInfo()
8690
}
8791

8892
let isSyncComplete = mutex.withLock {

Sources/AblyLiveObjects/Internal/DefaultLiveCounter.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ internal final class DefaultLiveCounter: LiveCounter {
6969
// RTLC5b: If the channel is in the DETACHED or FAILED state, the library should indicate an error with code 90001
7070
let currentChannelState = coreSDK.channelState
7171
if currentChannelState == .detached || currentChannelState == .failed {
72-
throw ARTErrorInfo.create(withCode: Int(ARTErrorCode.channelOperationFailedInvalidState.rawValue), message: "LiveCounter.value operation failed (invalid channel state: \(currentChannelState))")
72+
throw LiveObjectsError.objectsOperationFailedInvalidChannelState(
73+
operationDescription: "LiveCounter.value",
74+
channelState: currentChannelState,
75+
)
76+
.toARTErrorInfo()
7377
}
7478

7579
return mutex.withLock {

Sources/AblyLiveObjects/Internal/DefaultLiveMap.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ internal final class DefaultLiveMap: LiveMap {
108108
// RTLM5c: If the channel is in the DETACHED or FAILED state, the library should indicate an error with code 90001
109109
let currentChannelState = coreSDK.channelState
110110
if currentChannelState == .detached || currentChannelState == .failed {
111-
throw ARTErrorInfo.create(withCode: Int(ARTErrorCode.channelOperationFailedInvalidState.rawValue), message: "LiveMap.get operation failed (invalid channel state: \(currentChannelState))")
111+
throw LiveObjectsError.objectsOperationFailedInvalidChannelState(
112+
operationDescription: "LiveMap.get",
113+
channelState: currentChannelState,
114+
)
115+
.toARTErrorInfo()
112116
}
113117

114118
let entry = mutex.withLock {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Ably
2+
3+
/**
4+
Describes the errors that can be thrown by the LiveObjects SDK. Use ``toARTErrorInfo()`` to convert to an `ARTErrorInfo` that you can throw.
5+
*/
6+
internal enum LiveObjectsError {
7+
// operationDescription should be a description of a method like "LiveCounter.value"; it will be interpolated into an error message
8+
case objectsOperationFailedInvalidChannelState(operationDescription: String, channelState: ARTRealtimeChannelState)
9+
10+
/// The ``ARTErrorInfo/code`` that should be returned for this error.
11+
internal var code: ARTErrorCode {
12+
switch self {
13+
case .objectsOperationFailedInvalidChannelState:
14+
.channelOperationFailedInvalidState
15+
}
16+
}
17+
18+
/// The ``ARTErrorInfo/statusCode`` that should be returned for this error.
19+
internal var statusCode: Int {
20+
switch self {
21+
case .objectsOperationFailedInvalidChannelState:
22+
400
23+
}
24+
}
25+
26+
/// The ``ARTErrorInfo/localizedDescription`` that should be returned for this error.
27+
internal var localizedDescription: String {
28+
switch self {
29+
case let .objectsOperationFailedInvalidChannelState(operationDescription: operationDescription, channelState: channelState):
30+
"\(operationDescription) operation failed (invalid channel state: \(channelState))"
31+
}
32+
}
33+
34+
internal func toARTErrorInfo() -> ARTErrorInfo {
35+
ARTErrorInfo.create(
36+
withCode: Int(code.rawValue),
37+
status: statusCode,
38+
message: localizedDescription,
39+
)
40+
}
41+
}

Tests/AblyLiveObjectsTests/DefaultLiveCounterTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct DefaultLiveCounterTests {
1818
return false
1919
}
2020

21-
return errorInfo.code == 90001
21+
return errorInfo.code == 90001 && errorInfo.statusCode == 400
2222
}
2323
}
2424

Tests/AblyLiveObjectsTests/DefaultLiveMapTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct DefaultLiveMapTests {
1818
return false
1919
}
2020

21-
return errorInfo.code == 90001
21+
return errorInfo.code == 90001 && errorInfo.statusCode == 400
2222
}
2323
}
2424

Tests/AblyLiveObjectsTests/DefaultRealtimeObjectsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ struct DefaultRealtimeObjectsTests {
660660
return false
661661
}
662662

663-
return errorInfo.code == 90001
663+
return errorInfo.code == 90001 && errorInfo.statusCode == 400
664664
}
665665
}
666666
}

0 commit comments

Comments
 (0)