@@ -53,7 +53,7 @@ internal class DataConnectGrpcClient(
53
53
54
54
data class OperationResult (
55
55
val data : Struct ? ,
56
- val errors : List <DataConnectOperationFailureResponse . ErrorInfo >,
56
+ val errors : List <DataConnectOperationFailureResponseImpl . ErrorInfoImpl >,
57
57
)
58
58
59
59
suspend fun executeQuery (
@@ -161,25 +161,49 @@ internal object DataConnectGrpcClientGlobals {
161
161
fun <T > DataConnectGrpcClient.OperationResult.deserialize (
162
162
deserializer : DeserializationStrategy <T >,
163
163
serializersModule : SerializersModule ? ,
164
- ): T =
164
+ ): T {
165
165
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
184
167
}
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
+ }
185
209
}
0 commit comments