Skip to content

Commit 790a0c0

Browse files
committed
Translating Java assertions
1 parent b2a2599 commit 790a0c0

File tree

1 file changed

+205
-53
lines changed

1 file changed

+205
-53
lines changed

tests/nexus/test_workflow_caller.py

Lines changed: 205 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,47 +1164,211 @@ async def assert_handler_workflow_has_link_to_caller_workflow(
11641164
# io.temporal.failure.ApplicationFailure(type=no-type-attr, message=message='application error 1', type='APPLICATION_ERROR', nonRetryable=true)
11651165
# io.temporal.failure.ApplicationFailure(type=no-type-attr, message=message='Custom error 2', type='io.temporal.samples.nexus.handler.NexusServiceImpl$MyCustomException', nonRetryable=false)
11661166

1167-
# @OperationImpl
1168-
# public OperationHandler<NexusService.ErrorTestInput, NexusService.ErrorTestOutput> testError() {
1169-
# return OperationHandler.sync(
1170-
# (ctx, details, input) -> {
1171-
# switch (input.getAction()) {
1172-
# case RAISE_APPLICATION_ERROR:
1173-
# throw ApplicationFailure.newNonRetryableFailure(
1174-
# "application error 1", "APPLICATION_ERROR");
1175-
# case RAISE_CUSTOM_ERROR:
1176-
# throw new MyCustomException("Custom error 1");
1177-
# case RAISE_CUSTOM_ERROR_WITH_CAUSE_OF_CUSTOM_ERROR:
1178-
# // ** THIS DOESN'T WORK **: CHAINED CUSTOM EXCEPTIONS DON'T SERIALIZE
1179-
# MyCustomException customError = new MyCustomException("Custom error 1");
1180-
# customError.initCause(new MyCustomException("Custom error 2"));
1181-
# throw customError;
1182-
# case RAISE_APPLICATION_ERROR_WITH_CAUSE_OF_CUSTOM_ERROR:
1183-
# throw ApplicationFailure.newNonRetryableFailureWithCause(
1184-
# "application error 1",
1185-
# "APPLICATION_ERROR",
1186-
# new MyCustomException("Custom error 2"));
1187-
# case RAISE_NEXUS_HANDLER_ERROR:
1188-
# throw new HandlerException(HandlerException.ErrorType.NOT_FOUND, "Handler error 1");
1189-
# case RAISE_NEXUS_HANDLER_ERROR_WITH_CAUSE_OF_CUSTOM_ERROR:
1190-
# // ** THIS DOESN'T WORK **
1191-
# // Can't overwrite cause with
1192-
# // io.temporal.samples.nexus.handler.NexusServiceImpl$MyCustomException: Custom error
1193-
# // 2
1194-
# HandlerException handlerErr =
1195-
# new HandlerException(HandlerException.ErrorType.NOT_FOUND, "Handler error 1");
1196-
# handlerErr.initCause(new MyCustomException("Custom error 2"));
1197-
# throw handlerErr;
1198-
# case RAISE_NEXUS_OPERATION_ERROR_WITH_CAUSE_OF_CUSTOM_ERROR:
1199-
# throw OperationException.failure(
1200-
# ApplicationFailure.newNonRetryableFailureWithCause(
1201-
# "application error 1",
1202-
# "APPLICATION_ERROR",
1203-
# new MyCustomException("Custom error 2")));
1204-
# }
1205-
# return new NexusService.ErrorTestOutput("Unreachable");
1206-
# });
1207-
# }
1167+
1168+
@dataclass
1169+
class ErrorConversionTestCase:
1170+
name: str
1171+
java_behavior: list[tuple[type[Exception], dict[str, Any]]]
1172+
1173+
1174+
error_conversion_test_cases = []
1175+
1176+
1177+
# application_error_non_retryable:
1178+
_ = ["NexusOperationError", "HandlerError"]
1179+
# Java
1180+
_ = [
1181+
"NexusOperationError",
1182+
"HandlerError('handler error: application error', type='APPLICATION_ERROR', non_retryable=True)",
1183+
"ApplicationError('application error', type='APPLICATION_ERROR', non_retryable=True)",
1184+
]
1185+
1186+
error_conversion_test_cases.append(
1187+
ErrorConversionTestCase(
1188+
name="application_error_non_retryable",
1189+
java_behavior=[
1190+
(NexusOperationError, {}),
1191+
(
1192+
nexusrpc.HandlerError,
1193+
{
1194+
"message": "application error",
1195+
"type": "APPLICATION_ERROR",
1196+
"non_retryable": True,
1197+
},
1198+
),
1199+
(
1200+
ApplicationError,
1201+
{
1202+
"message": "application error",
1203+
"type": "APPLICATION_ERROR",
1204+
"non_retryable": True,
1205+
},
1206+
),
1207+
],
1208+
)
1209+
)
1210+
1211+
1212+
# custom_error:
1213+
_ = ["NexusOperationError", "HandlerError"]
1214+
# Java
1215+
_ = [
1216+
"NexusOperationError",
1217+
"HandlerError('Custom error wrapped: custom error', type='CUSTOM_ERROR', non_retryable=True)",
1218+
"ApplicationError('Custom error wrapped: custom error', type='CUSTOM_ERROR', non_retryable=True)",
1219+
]
1220+
error_conversion_test_cases.append(
1221+
ErrorConversionTestCase(
1222+
name="custom_error",
1223+
java_behavior=[
1224+
(NexusOperationError, {}),
1225+
(
1226+
nexusrpc.HandlerError,
1227+
{
1228+
"message": "Custom error wrapped: custom error",
1229+
"type": "CUSTOM_ERROR",
1230+
"non_retryable": True,
1231+
},
1232+
),
1233+
(
1234+
ApplicationError,
1235+
{
1236+
"message": "Custom error wrapped: custom error",
1237+
"type": "CUSTOM_ERROR",
1238+
"non_retryable": True,
1239+
},
1240+
),
1241+
],
1242+
)
1243+
)
1244+
1245+
1246+
# custom_error_from_custom_error:
1247+
_ = ["NexusOperationError", "HandlerError"]
1248+
# Java
1249+
# [Not possible]
1250+
1251+
# application_error_non_retryable_from_custom_error:
1252+
_ = ["NexusOperationError", "HandlerError"]
1253+
# Java
1254+
_ = [
1255+
"NexusOperationError",
1256+
"HandlerError('handler error: application error', type='APPLICATION_ERROR', non_retryable=True)",
1257+
"ApplicationError('application error', type='APPLICATION_ERROR', non_retryable=True)",
1258+
"ApplicationError('custom error', type='MyCustomException', non_retryable=False)",
1259+
]
1260+
1261+
error_conversion_test_cases.append(
1262+
ErrorConversionTestCase(
1263+
name="application_error_non_retryable_from_custom_error",
1264+
java_behavior=[
1265+
(NexusOperationError, {}),
1266+
(
1267+
nexusrpc.HandlerError,
1268+
{
1269+
"message": "handler error: application error",
1270+
"type": "APPLICATION_ERROR",
1271+
"non_retryable": True,
1272+
},
1273+
),
1274+
(
1275+
ApplicationError,
1276+
{
1277+
"message": "application error",
1278+
"type": "APPLICATION_ERROR",
1279+
"non_retryable": True,
1280+
},
1281+
),
1282+
(
1283+
ApplicationError,
1284+
{
1285+
"message": "custom error",
1286+
"type": "MyCustomException",
1287+
"non_retryable": False,
1288+
},
1289+
),
1290+
],
1291+
)
1292+
)
1293+
1294+
# nexus_handler_error_not_found:
1295+
_ = ["NexusOperationError", "HandlerError"]
1296+
# Java
1297+
_ = [
1298+
"NexusOperationError",
1299+
"HandlerError('handler error: handler error', type='RuntimeException', non_retryable=False)",
1300+
"ApplicationError('handler error', type='RuntimeException', non_retryable=False)",
1301+
]
1302+
1303+
error_conversion_test_cases.append(
1304+
ErrorConversionTestCase(
1305+
name="application_error_non_retryable_from_custom_error",
1306+
java_behavior=[
1307+
(NexusOperationError, {}),
1308+
(
1309+
nexusrpc.HandlerError,
1310+
{
1311+
"message": "handler error: handler error",
1312+
"type": "RuntimeException",
1313+
"non_retryable": False,
1314+
},
1315+
),
1316+
(
1317+
ApplicationError,
1318+
{
1319+
"message": "handler error",
1320+
"type": "RuntimeException",
1321+
"non_retryable": False,
1322+
},
1323+
),
1324+
],
1325+
)
1326+
)
1327+
1328+
# nexus_handler_error_not_found_from_custom_error:
1329+
_ = ["NexusOperationError", "HandlerError"]
1330+
# Java
1331+
# [Not possible]
1332+
error_conversion_test_cases.append(
1333+
ErrorConversionTestCase(
1334+
name="nexus_handler_error_not_found",
1335+
java_behavior=[], # [Not possible]
1336+
)
1337+
)
1338+
1339+
1340+
# nexus_operation_error_from_application_error_non_retryable_from_custom_error:
1341+
_ = ["NexusOperationError", "ApplicationError", "ApplicationError", "ApplicationError"]
1342+
# Java
1343+
_ = [
1344+
"NexusOperationError",
1345+
"ApplicationError('application error', type='APPLICATION_ERROR', non_retryable=True)",
1346+
"ApplicationError('custom error', type='MyCustomException', non_retryable=False)",
1347+
]
1348+
error_conversion_test_cases.append(
1349+
ErrorConversionTestCase(
1350+
name="nexus_operation_error_from_application_error_non_retryable_from_custom_error",
1351+
java_behavior=[
1352+
(NexusOperationError, {}),
1353+
(
1354+
ApplicationError,
1355+
{
1356+
"message": "application error",
1357+
"type": "APPLICATION_ERROR",
1358+
"non_retryable": True,
1359+
},
1360+
),
1361+
(
1362+
ApplicationError,
1363+
{
1364+
"message": "custom error",
1365+
"type": "MyCustomException",
1366+
"non_retryable": False,
1367+
},
1368+
),
1369+
],
1370+
)
1371+
)
12081372

12091373

12101374
ActionInSyncOp = Literal[
@@ -1344,15 +1508,3 @@ async def test_errors_raised_by_nexus_operation(
13441508
)
13451509

13461510
print(f"\n\n\n{action_in_sync_op}: \n", result, "\n\n\n")
1347-
1348-
# if action_in_sync_op == "handler_error":
1349-
# assert result == ["NexusOperationError", "HandlerError"]
1350-
# elif action_in_sync_op == "operation_error":
1351-
# assert result == ["NexusOperationError", "ApplicationError"]
1352-
# elif action_in_sync_op == "custom_error":
1353-
# # assert result == ["NexusOperationError", "CustomError"]
1354-
# pass
1355-
# else:
1356-
# raise NotImplementedError(
1357-
# f"Unhandled action_in_sync_op: {action_in_sync_op}"
1358-
# )

0 commit comments

Comments
 (0)