Skip to content

Commit b3235ac

Browse files
feat!: Match error codes to spec
1 parent 0308d42 commit b3235ac

3 files changed

Lines changed: 21 additions & 40 deletions

File tree

Sources/GraphQLTransportWS/GraphqlTransportWSError.swift

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,80 +9,73 @@ struct GraphQLTransportWSError: Error {
99
self.code = code
1010
}
1111

12-
static func unauthorized() -> Self {
12+
static func forbidden() -> Self {
1313
return self.init(
14-
"Unauthorized",
15-
code: .unauthorized
14+
"Forbidden",
15+
code: .forbidden
1616
)
1717
}
1818

1919
static func notInitialized() -> Self {
2020
return self.init(
2121
"Connection not initialized",
22-
code: .notInitialized
22+
code: .unauthorized
2323
)
2424
}
2525

2626
static func tooManyInitializations() -> Self {
2727
return self.init(
2828
"Too many initialisation requests",
29-
code: .tooManyInitializations
29+
code: .tooManyRequests
3030
)
3131
}
3232

3333
static func subscriberAlreadyExists(id: String) -> Self {
3434
return self.init(
3535
"Subscriber for \(id) already exists",
36-
code: .subscriberAlreadyExists
36+
code: .conflict
3737
)
3838
}
3939

4040
static func invalidEncoding() -> Self {
4141
return self.init(
4242
"Message was not encoded in UTF8",
43-
code: .invalidEncoding
43+
code: .miscellaneous
4444
)
4545
}
4646

4747
static func noType() -> Self {
4848
return self.init(
4949
"Message has no 'type' field",
50-
code: .noType
50+
code: .miscellaneous
5151
)
5252
}
5353

5454
static func invalidType() -> Self {
5555
return self.init(
5656
"Message 'type' value does not match supported types",
57-
code: .invalidType
57+
code: .miscellaneous
5858
)
5959
}
6060

6161
static func invalidRequestFormat(messageType: RequestMessageType) -> Self {
6262
return self.init(
6363
"Request message doesn't match '\(messageType.type.rawValue)' JSON format",
64-
code: .invalidRequestFormat
64+
code: .miscellaneous
6565
)
6666
}
6767

6868
static func invalidResponseFormat(messageType: ResponseMessageType) -> Self {
6969
return self.init(
7070
"Response message doesn't match '\(messageType.type.rawValue)' JSON format",
71-
code: .invalidResponseFormat
71+
code: .miscellaneous
7272
)
7373
}
7474

7575
static func internalAPIStreamIssue(errors: [GraphQLError]) -> Self {
7676
return self.init(
7777
"API Response did not result in a stream type, contained errors\n \(errors.map { $0.message }.joined(separator: "\n"))",
78-
code: .internalAPIStreamIssue
79-
)
80-
}
81-
82-
static func graphQLError(_ error: Error) -> Self {
83-
return self.init(
84-
"\(error)",
85-
code: .graphQLError
78+
code: .internalServerError
8679
)
8780
}
8881
}
@@ -91,23 +84,11 @@ struct GraphQLTransportWSError: Error {
9184
public enum ErrorCode: Int, CustomStringConvertible, Sendable {
9285
/// Miscellaneous
9386
case miscellaneous = 4400
94-
95-
// Internal errors
96-
case graphQLError = 4401
97-
case internalAPIStreamIssue = 4402
98-
99-
// Message errors
100-
case invalidEncoding = 4410
101-
case noType = 4411
102-
case invalidType = 4412
103-
case invalidRequestFormat = 4413
104-
case invalidResponseFormat = 4414
105-
106-
// Initialization errors
107-
case unauthorized = 4430
108-
case notInitialized = 4431
109-
case tooManyInitializations = 4432
110-
case subscriberAlreadyExists = 4433
87+
case unauthorized = 4401
88+
case forbidden = 4403
89+
case conflict = 4409
90+
case tooManyRequests = 4429
91+
case internalServerError = 4500
11192

11293
public var description: String {
11394
return "\(rawValue)"

Sources/GraphQLTransportWS/Server.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ where
133133
do {
134134
initResult = try await onInit(connectionInitRequest.payload)
135135
} catch {
136-
try await self.error(.unauthorized())
136+
try await self.error(.forbidden())
137137
return
138138
}
139139
initialized = true

Tests/GraphQLTransportWSTests/GraphQLTransportWSTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct GraphqlTransportWSTests {
5252
#expect(
5353
error
5454
== TestMessengerError(
55-
code: ErrorCode.notInitialized.rawValue,
55+
code: ErrorCode.unauthorized.rawValue,
5656
message: "Connection not initialized"
5757
)
5858
)
@@ -99,8 +99,8 @@ struct GraphqlTransportWSTests {
9999
#expect(
100100
error
101101
== TestMessengerError(
102-
code: ErrorCode.unauthorized.rawValue,
103-
message: "Unauthorized"
102+
code: ErrorCode.forbidden.rawValue,
103+
message: "Forbidden"
104104
)
105105
)
106106
}

0 commit comments

Comments
 (0)