Skip to content

Commit 77ce8df

Browse files
committed
DataConnectGrpcClient.kt: re-write OperationResult.deserialize() to use DataConnectOperationException instead of DataConnectException
1 parent 9b4331a commit 77ce8df

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

firebase-dataconnect/src/main/kotlin/com/google/firebase/dataconnect/core/DataConnectGrpcClient.kt

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal class DataConnectGrpcClient(
5353

5454
data class OperationResult(
5555
val data: Struct?,
56-
val errors: List<DataConnectOperationFailureResponse.ErrorInfo>,
56+
val errors: List<DataConnectOperationFailureResponseImpl.ErrorInfoImpl>,
5757
)
5858

5959
suspend fun executeQuery(
@@ -161,25 +161,49 @@ internal object DataConnectGrpcClientGlobals {
161161
fun <T> DataConnectGrpcClient.OperationResult.deserialize(
162162
deserializer: DeserializationStrategy<T>,
163163
serializersModule: SerializersModule?,
164-
): T =
164+
): T {
165165
if (deserializer === DataConnectUntypedData) {
166-
@Suppress("UNCHECKED_CAST")
167-
DataConnectUntypedData(data?.toMap(), errors) as T
168-
} else if (data === null) {
169-
if (errors.isNotEmpty()) {
170-
throw DataConnectException("operation failed: errors=$errors")
171-
} else {
172-
throw DataConnectException("no data included in result")
173-
}
174-
} else if (errors.isNotEmpty()) {
175-
throw DataConnectException("operation failed: errors=$errors (data=$data)")
176-
} else {
177-
try {
178-
decodeFromStruct(data, deserializer, serializersModule)
179-
} catch (dataConnectException: DataConnectException) {
180-
throw dataConnectException
181-
} catch (throwable: Throwable) {
182-
throw DataConnectException("decoding response data failed: $throwable", throwable)
183-
}
166+
@Suppress("UNCHECKED_CAST") return DataConnectUntypedData(data?.toMap(), errors) as T
184167
}
168+
169+
val decodedData: Result<T>? =
170+
data?.let { data -> runCatching { decodeFromStruct(data, deserializer, serializersModule) } }
171+
172+
if (errors.isNotEmpty()) {
173+
throw DataConnectOperationException(
174+
"operation encountered errors during execution: $errors",
175+
response =
176+
DataConnectOperationFailureResponseImpl(
177+
rawData = data?.toMap(),
178+
data = decodedData?.getOrNull(),
179+
errors = errors,
180+
)
181+
)
182+
}
183+
184+
if (decodedData == null) {
185+
throw DataConnectOperationException(
186+
"no data was included in the response from the server",
187+
response =
188+
DataConnectOperationFailureResponseImpl(
189+
rawData = null,
190+
data = null,
191+
errors = emptyList(),
192+
)
193+
)
194+
}
195+
196+
return decodedData.getOrElse { exception ->
197+
throw DataConnectOperationException(
198+
"decoding data from the server's response failed: ${exception.message}",
199+
cause = exception,
200+
response =
201+
DataConnectOperationFailureResponseImpl(
202+
rawData = data?.toMap(),
203+
data = null,
204+
errors = emptyList(),
205+
)
206+
)
207+
}
208+
}
185209
}

0 commit comments

Comments
 (0)