Skip to content

Commit 46a4acd

Browse files
Denver feedback: Include error messages in the error code.
1 parent 6ddf092 commit 46a4acd

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Sources/DataConnectError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ public enum DataConnectError: Error {
3636
case invalidTimestampFormat
3737

3838
/// generic operation execution error
39-
case operationExecutionFailed
39+
case operationExecutionFailed(messages: String?)
4040
}

Sources/Internal/GrpcClient.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import SwiftProtobuf
2727
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
2828
typealias FirebaseDataConnectAsyncClient =
2929
Google_Firebase_Dataconnect_V1beta_ConnectorServiceAsyncClient
30+
typealias FirebaseDataConnectGraphqlError = Google_Firebase_Dataconnect_V1beta_GraphqlError
3031

3132
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
3233
actor GrpcClient: CustomStringConvertible {
@@ -151,7 +152,8 @@ actor GrpcClient: CustomStringConvertible {
151152

152153
// Not doing error decoding here
153154
guard results.errors.isEmpty else {
154-
throw DataConnectError.operationExecutionFailed
155+
throw DataConnectError
156+
.operationExecutionFailed(messages: createErrorJson(errors: results.errors))
155157
}
156158

157159
if let decodedResults = try codec.decode(result: results.data, asType: resultType) {
@@ -198,7 +200,8 @@ actor GrpcClient: CustomStringConvertible {
198200
.debug("executeMutation() receives response: \(resultsString, privacy: .private).")
199201

200202
guard results.errors.isEmpty else {
201-
throw DataConnectError.operationExecutionFailed
203+
throw DataConnectError
204+
.operationExecutionFailed(messages: createErrorJson(errors: results.errors))
202205
}
203206

204207
if let decodedResults = try codec.decode(result: results.data, asType: resultType) {
@@ -216,6 +219,22 @@ actor GrpcClient: CustomStringConvertible {
216219
}
217220
}
218221

222+
private func createErrorJson(errors: [FirebaseDataConnectGraphqlError]) -> String? {
223+
var errorMessages = [String]()
224+
for err in errors {
225+
errorMessages.append(err.message)
226+
}
227+
228+
do {
229+
let jsonEncoder = JSONEncoder()
230+
let jsonData = try jsonEncoder.encode(errorMessages)
231+
return String(data: jsonData, encoding: .utf8)
232+
} catch {
233+
DataConnectLogger.error("Error encoding partial error list")
234+
return nil
235+
}
236+
}
237+
219238
func createCallOptions() async -> CallOptions {
220239
var headers = HPACKHeaders()
221240

0 commit comments

Comments
 (0)