From 2c746b25a1e4676c9b1595a33fe3bd76b3bef6c0 Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Mon, 6 Jul 2026 19:35:55 -0700 Subject: [PATCH] fix(cloudwatch): return ResourceNotFound for SetAlarmState on missing alarm (#1621) * fix: return ResourceNotFound 400 for SetAlarmState on missing alarm * fix: use CloudWatch ResourceNotFound error code for missing alarm * fix: use HTTP 404 for CloudWatch ResourceNotFound Per @hectorvent's review: the CloudWatch service model (botocore cloudwatch/2010-08-01 service-2.json) declares the ResourceNotFound shape with httpStatusCode 404, so correct the AwsException status from 400 to 404 for the missing-alarm SetAlarmState path and update the test assertion to match. --------- Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> --- .../cloudwatch/metrics/CloudWatchMetricsService.java | 3 ++- .../metrics/CloudWatchMetricsServiceTest.java | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java index bf17a75434..b9d9476f9a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.cloudwatch.metrics; +import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -266,7 +267,7 @@ public class CloudWatchMetricsService { public void setAlarmState(String alarmName, String stateValue, String stateReason, String stateReasonData, String region) { String key = region + "::" + alarmName; MetricAlarm alarm = alarmStore.get(key) - .orElseThrow(() -> new RuntimeException("Alarm not found: " + alarmName)); + .orElseThrow(() -> new AwsException("ResourceNotFound", "Alarm not found: " + alarmName, 404)); alarm.setStateValue(stateValue); alarm.setStateReason(stateReason); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java index 969ba0356f..ec23f2084c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.cloudwatch.metrics; +import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension; @@ -229,6 +230,15 @@ class CloudWatchMetricsServiceTest { assertEquals("Threshold breached", updated.getStateReason()); } + @Test + void setAlarmStateOnNonexistentAlarmThrowsResourceNotFound() { + AwsException ex = assertThrows(AwsException.class, + () -> service.setAlarmState("missing-alarm", "ALARM", "reason", null, REGION)); + + assertEquals(404, ex.getHttpStatus()); + assertEquals("ResourceNotFound", ex.getErrorCode()); + } + @Test void buildDimKeyIsDeterministic() { List dims1 = List.of(new Dimension("B", "2"), new Dimension("A", "1")); @@ -243,4 +253,4 @@ class CloudWatchMetricsServiceTest { assertEquals("", CloudWatchMetricsService.buildDimKey(List.of())); assertEquals("", CloudWatchMetricsService.buildDimKey(null)); } -} \ No newline at end of file +}