Skip to content

Commit 80413a2

Browse files
refactor: Move error to messenger
1 parent dc371dc commit 80413a2

3 files changed

Lines changed: 23 additions & 26 deletions

File tree

Sources/GraphQLTransportWS/Client.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public actor Client<InitPayload: Equatable & Codable> {
5050
do {
5151
response = try decoder.decode(Response.self, from: message)
5252
} catch {
53-
try await self.error(.noType())
53+
try await messenger.error(.noType())
5454
return
5555
}
5656

@@ -62,31 +62,31 @@ public actor Client<InitPayload: Equatable & Codable> {
6262
from: message
6363
)
6464
else {
65-
try await error(.invalidResponseFormat(messageType: .connectionAck))
65+
try await messenger.error(.invalidResponseFormat(messageType: .connectionAck))
6666
return
6767
}
6868
try await onConnectionAck(connectionAckResponse, self)
6969
case .next:
7070
guard let nextResponse = try? decoder.decode(NextResponse.self, from: message) else {
71-
try await error(.invalidResponseFormat(messageType: .next))
71+
try await messenger.error(.invalidResponseFormat(messageType: .next))
7272
return
7373
}
7474
try await onNext(nextResponse, self)
7575
case .error:
7676
guard let errorResponse = try? decoder.decode(ErrorResponse.self, from: message) else {
77-
try await error(.invalidResponseFormat(messageType: .error))
77+
try await messenger.error(.invalidResponseFormat(messageType: .error))
7878
return
7979
}
8080
try await onError(errorResponse, self)
8181
case .complete:
8282
guard let completeResponse = try? decoder.decode(CompleteResponse.self, from: message)
8383
else {
84-
try await error(.invalidResponseFormat(messageType: .complete))
84+
try await messenger.error(.invalidResponseFormat(messageType: .complete))
8585
return
8686
}
8787
try await onComplete(completeResponse, self)
8888
default:
89-
try await error(.invalidType())
89+
try await messenger.error(.invalidType())
9090
}
9191
}
9292

@@ -123,9 +123,4 @@ public actor Client<InitPayload: Equatable & Codable> {
123123
)
124124
)
125125
}
126-
127-
/// Send an error through the messenger and close the connection
128-
private func error(_ error: GraphQLTransportWSError) async throws {
129-
try await messenger.error(error.message, code: error.code.rawValue)
130-
}
131126
}

Sources/GraphQLTransportWS/Messenger.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ public protocol Messenger: Sendable {
1515
/// - code: An error code
1616
func error(_ message: String, code: Int) async throws
1717
}
18+
19+
extension Messenger {
20+
/// Send an error through the messenger and close the connection
21+
func error(_ error: GraphQLTransportWSError) async throws {
22+
try await self.error(error.message, code: error.code.rawValue)
23+
}
24+
}

Sources/GraphQLTransportWS/Server.swift

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where
7070
do {
7171
request = try decoder.decode(Request.self, from: message)
7272
} catch {
73-
try await self.error(.noType())
73+
try await messenger.error(.noType())
7474
return
7575
}
7676

@@ -83,26 +83,26 @@ where
8383
from: message
8484
)
8585
else {
86-
try await error(.invalidRequestFormat(messageType: .connectionInit))
86+
try await messenger.error(.invalidRequestFormat(messageType: .connectionInit))
8787
return
8888
}
8989
try await onConnectionInit(connectionInitRequest, messenger)
9090
case .subscribe:
9191
guard let subscribeRequest = try? decoder.decode(SubscribeRequest.self, from: message)
9292
else {
93-
try await error(.invalidRequestFormat(messageType: .subscribe))
93+
try await messenger.error(.invalidRequestFormat(messageType: .subscribe))
9494
return
9595
}
9696
try await onSubscribe(subscribeRequest)
9797
case .complete:
9898
guard let completeRequest = try? decoder.decode(CompleteRequest.self, from: message)
9999
else {
100-
try await error(.invalidRequestFormat(messageType: .complete))
100+
try await messenger.error(.invalidRequestFormat(messageType: .complete))
101101
return
102102
}
103103
try await onOperationComplete(completeRequest)
104104
default:
105-
try await error(.invalidType())
105+
try await messenger.error(.invalidType())
106106
}
107107
}
108108

@@ -111,14 +111,14 @@ where
111111
_: Messenger
112112
) async throws {
113113
guard !initialized else {
114-
try await error(.tooManyInitializations())
114+
try await messenger.error(.tooManyInitializations())
115115
return
116116
}
117117

118118
do {
119119
initResult = try await onInit(connectionInitRequest.payload)
120120
} catch {
121-
try await self.error(.forbidden())
121+
try await messenger.error(.forbidden())
122122
return
123123
}
124124
initialized = true
@@ -128,7 +128,7 @@ where
128128

129129
private func onSubscribe(_ subscribeRequest: SubscribeRequest) async throws {
130130
guard initialized, let initResult else {
131-
try await error(.notInitialized())
131+
try await messenger.error(.notInitialized())
132132
return
133133
}
134134

@@ -144,7 +144,7 @@ where
144144
}
145145

146146
guard executionTasks[id] == nil else {
147-
try await self.error(.subscriberAlreadyExists(id: id))
147+
try await messenger.error(.subscriberAlreadyExists(id: id))
148148
return
149149
}
150150
executionTasks[id] = Task {
@@ -181,7 +181,7 @@ where
181181

182182
private func onOperationComplete(_ completeRequest: CompleteRequest) async throws {
183183
guard initialized else {
184-
try await error(.notInitialized())
184+
try await messenger.error(.notInitialized())
185185
return
186186
}
187187

@@ -243,9 +243,4 @@ where
243243
private func sendError(_ error: Error, id: String) async throws {
244244
try await sendError([error], id: id)
245245
}
246-
247-
/// Send an error through the messenger and close the connection
248-
private func error(_ error: GraphQLTransportWSError) async throws {
249-
try await messenger.error(error.message, code: error.code.rawValue)
250-
}
251246
}

0 commit comments

Comments
 (0)