diff options
| author | Christian Cleberg <[email protected]> | 2026-04-13 14:20:27 -0500 |
|---|---|---|
| committer | Christian Cleberg <[email protected]> | 2026-04-13 14:20:27 -0500 |
| commit | 5e63048ab6a741ae947f7fe42efcb5206a3f3963 (patch) | |
| tree | cf8eb2e3bc3b2edb20ef96a2599e684378120e5b /Hutch/Networking/SRHTClient.swift | |
| parent | 0b84ecaf7ba0fb2cd3f3867da8dc732450b55b50 (diff) | |
| download | hutch-3.0.4.tar.gz hutch-3.0.4.tar.bz2 hutch-3.0.4.zip | |
fix: mercurial parity and graphql handlingv3.0.4
- implements consistent UX between repo types
- ensures centralized services and routes are used between repo types
- graphql error handling is centralized
- error messages are consistent
Implements: https://todo.sr.ht/~ccleberg/hutch/58
Implements: https://todo.sr.ht/~ccleberg/hutch/59
Diffstat (limited to 'Hutch/Networking/SRHTClient.swift')
| -rw-r--r-- | Hutch/Networking/SRHTClient.swift | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/Hutch/Networking/SRHTClient.swift b/Hutch/Networking/SRHTClient.swift index 1b8b9b3..7e1f619 100644 --- a/Hutch/Networking/SRHTClient.swift +++ b/Hutch/Networking/SRHTClient.swift @@ -89,19 +89,12 @@ final class SRHTClient: Sendable { throw SRHTError.unauthorized } if !(200...299).contains(http.statusCode) { - // Try to extract GraphQL errors from the response body even on non-2xx - if let gqlResponse = try? decoder.decode(GraphQLResponse<EmptyData>.self, from: data), - let errors = gqlResponse.errors, !errors.isEmpty { - throw SRHTError.graphQLErrors(errors) - } + try throwGraphQLErrorsIfPresent(in: data) throw SRHTError.httpError(http.statusCode) } } - if let errorEnvelope = try? decoder.decode(GraphQLResponse<EmptyData>.self, from: data), - let errors = errorEnvelope.errors, !errors.isEmpty { - throw SRHTError.graphQLErrors(errors) - } + try throwGraphQLErrorsIfPresent(in: data) // Decode GraphQL response envelope let graphQLResponse: GraphQLResponse<T> @@ -249,14 +242,12 @@ final class SRHTClient: Sendable { throw SRHTError.unauthorized } if !(200...299).contains(http.statusCode) { + try throwGraphQLErrorsIfPresent(in: data) throw SRHTError.httpError(http.statusCode) } } - if let errorEnvelope = try? decoder.decode(GraphQLResponse<EmptyData>.self, from: data), - let errors = errorEnvelope.errors, !errors.isEmpty { - throw SRHTError.graphQLErrors(errors) - } + try throwGraphQLErrorsIfPresent(in: data) let graphQLResponse: GraphQLResponse<T> do { @@ -381,14 +372,13 @@ final class SRHTClient: Sendable { throw SRHTError.unauthorized } if !(200...299).contains(http.statusCode) { - if let gqlResponse = try? decoder.decode(GraphQLResponse<EmptyData>.self, from: data), - let errors = gqlResponse.errors, !errors.isEmpty { - throw SRHTError.graphQLErrors(errors) - } + try throwGraphQLErrorsIfPresent(in: data) throw SRHTError.httpError(http.statusCode) } } + try throwGraphQLErrorsIfPresent(in: data) + let graphQLResponse: GraphQLResponse<T> do { graphQLResponse = try decoder.decode(GraphQLResponse<T>.self, from: data) @@ -474,6 +464,7 @@ final class SRHTClient: Sendable { throw SRHTError.unauthorized } if !(200...299).contains(http.statusCode) { + try throwGraphQLErrorsIfPresent(in: data) throw SRHTError.httpError(http.statusCode) } } @@ -627,6 +618,14 @@ final class SRHTClient: Sendable { // MARK: - Data Helper private extension SRHTClient { + func throwGraphQLErrorsIfPresent(in data: Data) throws { + if let envelope = try? decoder.decode(GraphQLResponse<EmptyData>.self, from: data), + let errors = envelope.errors, + !errors.isEmpty { + throw SRHTError.graphQLErrors(errors) + } + } + static func isTrustedAuthenticatedTextURL(_ url: URL) -> Bool { guard url.scheme?.localizedCaseInsensitiveCompare("https") == .orderedSame, let host = url.host?.lowercased() else { |
