Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/DataConnectError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ public enum DataConnectError: Error {

/// timestamp components specified to initialize Timestamp are invalid
case invalidTimestampFormat

/// generic operation execution error
case operationExecutionFailed(messages: String?)
}
29 changes: 29 additions & 0 deletions Sources/Internal/GrpcClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import SwiftProtobuf
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
typealias FirebaseDataConnectAsyncClient =
Google_Firebase_Dataconnect_V1beta_ConnectorServiceAsyncClient
typealias FirebaseDataConnectGraphqlError = Google_Firebase_Dataconnect_V1beta_GraphqlError

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
actor GrpcClient: CustomStringConvertible {
Expand Down Expand Up @@ -148,7 +149,13 @@ actor GrpcClient: CustomStringConvertible {
let resultsString = try results.jsonString()
DataConnectLogger
.debug("executeQuery() receives response: \(resultsString, privacy: .private).")

// Not doing error decoding here
guard results.errors.isEmpty else {
throw DataConnectError
.operationExecutionFailed(messages: createErrorJson(errors: results.errors))
}

if let decodedResults = try codec.decode(result: results.data, asType: resultType) {
return OperationResult(data: decodedResults)
} else {
Expand Down Expand Up @@ -191,6 +198,12 @@ actor GrpcClient: CustomStringConvertible {
let resultsString = try results.jsonString()
DataConnectLogger
.debug("executeMutation() receives response: \(resultsString, privacy: .private).")

guard results.errors.isEmpty else {
throw DataConnectError
.operationExecutionFailed(messages: createErrorJson(errors: results.errors))
}

if let decodedResults = try codec.decode(result: results.data, asType: resultType) {
return OperationResult(data: decodedResults)
} else {
Expand All @@ -206,6 +219,22 @@ actor GrpcClient: CustomStringConvertible {
}
}

private func createErrorJson(errors: [FirebaseDataConnectGraphqlError]) -> String? {
var errorMessages = [String]()
for err in errors {
errorMessages.append(err.message)
}

do {
let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(errorMessages)
return String(data: jsonData, encoding: .utf8)
} catch {
DataConnectLogger.error("Error encoding partial error list")
return nil
}
}

func createCallOptions() async -> CallOptions {
var headers = HPACKHeaders()

Expand Down
Loading