fix(emr): return AWS client-error codes instead of 500 InternalServerError (#1731)

* fix(emr): return AWS client-error codes instead of 500 InternalServerError

Three EMR paths returned 500 InternalServerError for pure client errors,
which SDK clients treat as retryable server faults. Verified against the
botocore model and moto: RunJobFlow with a missing Name now returns 400
ValidationException in the AWS validation-message format, AddJobFlowSteps
with an unknown cluster returns 400 InvalidRequestException with the
'Cluster id ... is not valid.' message already used by DescribeCluster,
and TerminateJobFlows now matches AWS semantics by terminating the
unprotected clusters in the request before failing with 400
ValidationException when any cluster is termination protected.

* fix(emr): reject only a missing Name and pin the validation message

Review follow-up: the blank-name branch produced a message claiming the value
was null, and rejecting an explicit empty string was itself stricter than AWS.
The Name member is REQUIRED with a len=[0..256] constraint, so real EMR accepts
an empty string and framework validation only rejects null. Restrict the check
to null, pin the validation message in the missing-Name test, and add a test
that an empty Name creates a cluster.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-07-05 09:48:34 -05:00
1 parent 71ee94d8b0
commit 87749e4600
2 files changed
+77 -5

No files matched your search

@@ -50,8 +50,12 @@ public class EmrService {
// ──────────────────────────── Cluster lifecycle ────────────────────────────
public EmrCluster runJobFlow(EmrCluster cluster, String region) {
if (cluster.getName() == null || cluster.getName().isBlank()) {
throw new AwsException("InternalServerError", "Name is required.", 500);
// Only a missing Name fails AWS's framework validation: the member is REQUIRED but its
// constraint is len=[0..256], so an explicit empty string is accepted by real EMR.
if (cluster.getName() == null) {
throw new AwsException("ValidationException",
"1 validation error detected: Value null at 'name' failed to satisfy constraint: "
+ "Member must not be null", 400);
}
String id = "j-" + randomId(13);
cluster.setId(id);
@@ -104,14 +108,17 @@ public class EmrService {
}
public void terminateJobFlows(List<String> ids) {
// AWS terminates the unprotected clusters in the request and then fails it with a
// ValidationException if any were termination protected.
boolean anyProtected = false;
for (String id : ids) {
EmrCluster cluster = clusterStore.get(id).orElse(null);
if (cluster == null) {
continue;
}
if (cluster.isTerminationProtected()) {
throw new AwsException("InternalServerError",
"Cluster " + id + " is protected from termination.", 500);
anyProtected = true;
continue;
}
if ("TERMINATED".equals(cluster.getState())) {
continue;
@@ -122,6 +129,10 @@ public class EmrService {
cluster.setEndDateTime(Instant.now());
clusterStore.put(id, cluster);
}
if (anyProtected) {
throw new AwsException("ValidationException",
"Could not shut down one or more job flows since they are termination protected.", 400);
}
}
public void setTerminationProtection(List<String> ids, boolean value) {
@@ -156,7 +167,7 @@ public class EmrService {
public List<String> addJobFlowSteps(String clusterId, List<EmrStep> steps) {
EmrCluster cluster = clusterStore.get(clusterId).orElseThrow(() -> new AwsException(
"InternalServerError", "Cluster " + clusterId + " does not exist.", 500));
"InvalidRequestException", "Cluster id '" + clusterId + "' is not valid.", 400));
List<String> ids = new ArrayList<>();
for (EmrStep step : steps) {
initStep(step);