Skip to content

Commit

Permalink
public initializers on error types (#19)
Browse files Browse the repository at this point in the history
* public initializers on error types
* swift6 fixes for iOS, and fixing fallout from error initializer updates
  • Loading branch information
heckj authored Apr 11, 2024
1 parent 6561c13 commit bc165bd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
24 changes: 24 additions & 0 deletions Sources/AutomergeRepo/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,64 @@ enum Errors: Sendable {
public var errorDescription: String? {
"NetworkProviderError: \(msg)"
}

public init(msg: String) {
self.msg = msg
}
}

public struct UnsupportedProtocolError: Sendable, LocalizedError {
public var msg: String
public var errorDescription: String? {
"Unsupported protocol requested: \(msg)"
}

public init(msg: String) {
self.msg = msg
}
}

public struct Unavailable: Sendable, LocalizedError {
let id: DocumentId
public var errorDescription: String? {
"Unknown document Id: \(id)"
}

public init(id: DocumentId) {
self.id = id
}
}

public struct DocDeleted: Sendable, LocalizedError {
let id: DocumentId
public var errorDescription: String? {
"Document with Id: \(id) has been deleted."
}

public init(id: DocumentId) {
self.id = id
}
}

public struct DocUnavailable: Sendable, LocalizedError {
let id: DocumentId
public var errorDescription: String? {
"Document with Id: \(id) is unavailable."
}

public init(id: DocumentId) {
self.id = id
}
}

public struct BigBadaBoom: Sendable, LocalizedError {
let msg: String
public var errorDescription: String? {
"Something went quite wrong: \(msg)."
}

public init(msg: String) {
self.msg = msg
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public actor PeerToPeerProvider: NetworkProvider {
// we were the initiating side of the connection.

guard case let .peer(peerMsg) = nextMessage else {
throw SyncV1Msg.Errors.UnexpectedMsg(msg: nextMessage)
throw SyncV1Msg.Errors.UnexpectedMsg(msg: nextMessage.debugDescription)
}

holder.peerId = peerMsg.senderId
Expand Down Expand Up @@ -659,7 +659,7 @@ public actor PeerToPeerProvider: NetworkProvider {
// we were the initiating side of the connection.

guard case let .join(joinMsg) = nextMessage else {
throw SyncV1Msg.Errors.UnexpectedMsg(msg: nextMessage)
throw SyncV1Msg.Errors.UnexpectedMsg(msg: nextMessage.debugDescription)
}

// send the peer candidate information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct PeerToPeerProviderConfiguration: Sendable {
let peerName: String
let passcode: String

init(reconnectOnError: Bool, listening: Bool, peerName: String?, passcode: String, autoconnect: Bool? = nil) {
init(reconnectOnError: Bool, listening: Bool, peerName: String?, passcode: String, autoconnect: Bool? = nil) async {
self.reconnectOnError = reconnectOnError
self.listening = listening
if let auto = autoconnect {
Expand All @@ -25,17 +25,19 @@ public struct PeerToPeerProviderConfiguration: Sendable {
if let name = peerName {
self.peerName = name
} else {
self.peerName = Self.defaultSharingIdentity()
self.peerName = await Self.defaultSharingIdentity()
}
self.passcode = passcode
}

// MARK: default sharing identity

public static func defaultSharingIdentity() -> String {
public static func defaultSharingIdentity() async -> String {
let defaultName: String
#if os(iOS)
defaultName = UIDevice().name
defaultName = await MainActor.run(body: {
UIDevice().name
})
#elseif os(macOS)
defaultName = Host.current().localizedName ?? "Automerge User"
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ public actor WebSocketProvider: NetworkProvider {
let decodeAttempted = SyncV1Msg.decode(raw_data)
Logger.webSocket
.warning(
"Decoding websocket message, expecting peer only - and it wasn't a peer message. RECEIVED MSG: \(decodeAttempted.debugDescription)"
"Decoding websocket message, expecting peer only - and it wasn't a peer message. RECEIVED MSG: \(String(describing: decodeAttempted))"
)
throw SyncV1Msg.Errors.UnexpectedMsg(msg: decodeAttempted)
throw SyncV1Msg.Errors.UnexpectedMsg(msg: String(describing: decodeAttempted))
}
} else {
let decodedMsg = SyncV1Msg.decode(raw_data)
if case .unknown = decodedMsg {
throw SyncV1Msg.Errors.UnexpectedMsg(msg: decodedMsg)
throw SyncV1Msg.Errors.UnexpectedMsg(msg: decodedMsg.debugDescription)
}
return decodedMsg
}
Expand All @@ -131,12 +131,12 @@ public actor WebSocketProvider: NetworkProvider {
// In the handshake phase and received anything other than a valid peer message
Logger.webSocket
.warning("Unknown websocket message received: .string(\(string))")
throw SyncV1Msg.Errors.UnexpectedMsg(msg: msg)
throw SyncV1Msg.Errors.UnexpectedMsg(msg: string)
@unknown default:
// In the handshake phase and received anything other than a valid peer message
Logger.webSocket
.error("Unknown websocket message received: \(String(describing: msg))")
throw SyncV1Msg.Errors.UnexpectedMsg(msg: msg)
throw SyncV1Msg.Errors.UnexpectedMsg(msg: String(describing: msg))
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public actor WebSocketProvider: NetworkProvider {
// For the sync protocol handshake phase, it's essentially "peer or die" since
// we were the initiating side of the connection.
guard case let .peer(peerMsg) = try attemptToDecode(websocketMsg, peerOnly: true) else {
throw SyncV1Msg.Errors.UnexpectedMsg(msg: websocketMsg)
throw SyncV1Msg.Errors.UnexpectedMsg(msg: String(describing: websocketMsg))
}

let newPeerConnection = PeerConnection(peerId: peerMsg.senderId, peerMetadata: peerMsg.peerMetadata)
Expand Down
12 changes: 10 additions & 2 deletions Sources/AutomergeRepo/Sync/SyncV1Msg+Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ public extension SyncV1Msg {
public var errorDescription: String? {
"Invalid URL: \(urlString)"
}

public init(urlString: String) {
self.urlString = urlString
}
}

public struct UnexpectedMsg<MSG: Sendable>: Sendable, LocalizedError {
public var msg: MSG
public struct UnexpectedMsg: Sendable, LocalizedError {
public var msg: String
public var errorDescription: String? {
"Received an unexpected message: \(msg)"
}

public init(msg: String) {
self.msg = msg
}
}

public struct DocumentUnavailable: Sendable, LocalizedError {
Expand Down

0 comments on commit bc165bd

Please sign in to comment.