Skip to content

Commit 6742670

Browse files
committed
Removed catch (NullReferenceException) -
1 parent f96c1d9 commit 6742670

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

libraries/src/AWS.Lambda.Powertools.Metrics/Model/MetricDirective.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,21 @@ public void AddMetric(string name, double value, MetricUnit unit, MetricResoluti
177177
}
178178

179179
var metric = GetExistingMetric(Metrics, name);
180-
if (metric != null)
180+
if (metric?.Values != null)
181181
{
182-
try
182+
if (metric.Values.Count < PowertoolsConfigurations.MaxMetrics)
183183
{
184-
if (metric.Values != null && metric.Values.Count < PowertoolsConfigurations.MaxMetrics)
185-
metric.AddValue(value);
186-
else
187-
throw new ArgumentOutOfRangeException(nameof(metric),
188-
$"Cannot add more than {PowertoolsConfigurations.MaxMetrics} metric data points at the same time.");
184+
metric.AddValue(value);
189185
}
190-
catch (NullReferenceException)
186+
else
191187
{
192-
// If metric became null due to concurrent access, create a new one
193-
Metrics.Add(new MetricDefinition(name, unit, value, metricResolution));
188+
throw new ArgumentOutOfRangeException(nameof(metric),
189+
$"Cannot add more than {PowertoolsConfigurations.MaxMetrics} metric data points at the same time.");
194190
}
195191
}
196192
else
197193
{
194+
// Either no existing metric found or metric/Values became null due to concurrent access
198195
Metrics.Add(new MetricDefinition(name, unit, value, metricResolution));
199196
}
200197
}

0 commit comments

Comments
 (0)