@@ -245,50 +245,58 @@ async def _run_nexus_operation(
245
245
# bytes details = 3;
246
246
# }
247
247
248
- completion = temporalio .bridge .proto .nexus .NexusTaskCompletion (
249
- task_token = task_token ,
250
- )
251
- try :
252
- result = await start (input , options )
253
- except BaseException :
254
- # await self._data_converter.encode_failure(err, completion.error.failure)
255
- # TODO(dan): mirror appropriate aspects of _run_activity error handling
256
- raise NotImplementedError (
257
- "TODO: Nexus operation error handling not implemented"
258
- )
259
-
260
- try :
261
- # Send task completion
262
- if isinstance (result , nexusrpc .handler .StartOperationAsyncResult ):
263
- print (f"🟢 Nexus operation started with async response { result } " )
264
- op_resp = temporalio .api .nexus .v1 .StartOperationResponse (
265
- async_success = temporalio .api .nexus .v1 .StartOperationResponse .Async (
266
- operation_token = result .token ,
267
- links = [
268
- temporalio .api .nexus .v1 .Link (url = l .url , type = l .type )
269
- for l in result .links
270
- ],
271
- )
248
+ async def run () -> temporalio .bridge .proto .nexus .NexusTaskCompletion :
249
+ try :
250
+ result = await start (input , options )
251
+ except BaseException as err :
252
+ # TODO(dan): should encode_failure be called here?? (It accepts the
253
+ # api.Failure proto struct, not the Nexus one.)
254
+ # await self._data_converter.encode_failure(err, completion.error.failure)
255
+ return temporalio .bridge .proto .nexus .NexusTaskCompletion (
256
+ task_token = task_token ,
257
+ error = temporalio .api .nexus .v1 .HandlerError (
258
+ # TODO(dan): what should error_type be? See `Predefined Handler Errors` in spec
259
+ error_type = "INTERNAL" ,
260
+ failure = temporalio .api .nexus .v1 .Failure (
261
+ message = str (err ),
262
+ metadata = {},
263
+ details = b"" ,
264
+ ),
265
+ ),
272
266
)
273
267
else :
274
- # TODO(dan): are we going to use StartOperationSyncResult from nexusrpc?
275
- # (contains links and headers in addition to result) IIRC Go does something
276
- # like that.
277
- [payload ] = await self ._data_converter .encode ([result ])
278
- op_resp = temporalio .api .nexus .v1 .StartOperationResponse (
279
- sync_success = temporalio .api .nexus .v1 .StartOperationResponse .Sync (
280
- payload = payload
268
+ if isinstance (result , nexusrpc .handler .StartOperationAsyncResult ):
269
+ print (f"🟢 Nexus operation started with async response { result } " )
270
+ op_resp = temporalio .api .nexus .v1 .StartOperationResponse (
271
+ async_success = temporalio .api .nexus .v1 .StartOperationResponse .Async (
272
+ operation_token = result .token ,
273
+ links = [
274
+ temporalio .api .nexus .v1 .Link (url = l .url , type = l .type )
275
+ for l in result .links
276
+ ],
277
+ )
278
+ )
279
+ else :
280
+ # TODO(dan): are we going to use StartOperationSyncResult from nexusrpc?
281
+ # (contains links and headers in addition to result) IIRC Go does something
282
+ # like that.
283
+ [payload ] = await self ._data_converter .encode ([result ])
284
+ op_resp = temporalio .api .nexus .v1 .StartOperationResponse (
285
+ sync_success = temporalio .api .nexus .v1 .StartOperationResponse .Sync (
286
+ payload = payload
287
+ )
281
288
)
289
+ return temporalio .bridge .proto .nexus .NexusTaskCompletion (
290
+ task_token = task_token ,
291
+ completed = temporalio .api .nexus .v1 .Response (start_operation = op_resp ),
282
292
)
283
- completion . completed . CopyFrom (
284
- temporalio . api . nexus . v1 . Response ( start_operation = op_resp )
285
- )
293
+
294
+ try :
295
+ completion = await run ( )
286
296
await self ._bridge_worker ().complete_nexus_task (completion )
287
297
del self ._running_operations [task_token ]
288
298
except Exception :
289
- temporalio .nexus .logger .exception (
290
- "Failed to send Nexus operation completion"
291
- )
299
+ temporalio .nexus .logger .exception ("Failed completing Nexus operation" )
292
300
293
301
async def _handle_cancel_operation (
294
302
self , request : temporalio .api .nexus .v1 .CancelOperationRequest , task_token : bytes
0 commit comments