Skip to content

Commit 0733b22

Browse files
committed
refactor: add default cases to switch statements for better error handling
1 parent 2bbb1e4 commit 0733b22

File tree

23 files changed

+62
-13
lines changed

23 files changed

+62
-13
lines changed

cmd/es-rollover/app/lookback/time_reference.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func getTimeReference(currentTime time.Time, units string, unitCount int) time.T
2525
case "years":
2626
year, month, day := currentTime.Date()
2727
return time.Date(year, month, day, 0, 0, 0, 0, currentTime.Location()).AddDate(-1*unitCount, 0, 0)
28+
default:
29+
return currentTime.Truncate(time.Second).Add(-time.Duration(unitCount) * time.Second)
2830
}
29-
return currentTime.Truncate(time.Second).Add(-time.Duration(unitCount) * time.Second)
3031
}

cmd/jaeger/internal/extension/jaegerstorage/extension.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ func (s *storageExt) Start(ctx context.Context, host component.Host) error {
195195
*cfg.Opensearch,
196196
osTelset,
197197
)
198+
default:
199+
// default case
198200
}
199201
if err != nil {
200202
return fmt.Errorf("failed to initialize storage '%s': %w", storageName, err)
@@ -232,6 +234,8 @@ func (s *storageExt) Start(ctx context.Context, host component.Host) error {
232234
*cfg.Opensearch,
233235
osTelset,
234236
)
237+
default:
238+
err = fmt.Errorf("no metric backend configuration provided for '%s'", metricStorageName)
235239
}
236240
if err != nil {
237241
return fmt.Errorf("failed to initialize metrics storage '%s': %w", metricStorageName, err)

cmd/jaeger/internal/extension/remotestorage/server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ func (fakeStorageExt) TraceStorageFactory(name string) (tracestore.Factory, bool
7777
return nil, false
7878
case "without-dependency-storage":
7979
return fakeTraceStorageFactory{name: name}, true
80+
default:
81+
return newFakeFactory(name), true
8082
}
81-
82-
return newFakeFactory(name), true
8383
}
8484

8585
func (fakeStorageExt) MetricStorageFactory(string) (storage.MetricStoreFactory, bool) {

examples/hotrod/pkg/tracing/rpcmetrics/metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func (m *Metrics) recordHTTPStatusCode(statusCode int64) {
5454
m.HTTPStatusCode4xx.Inc(1)
5555
case statusCode >= http.StatusInternalServerError && statusCode < 600:
5656
m.HTTPStatusCode5xx.Inc(1)
57+
default:
58+
// Status codes outside standard ranges (< 200 or >= 600) are ignored
5759
}
5860
}
5961

examples/hotrod/services/driver/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewServer(hostPort string, otelExporter string, metricsFactory metrics.Fact
4444

4545
// Run starts the Driver server
4646
func (s *Server) Run() error {
47-
lis, err := net.Listen("tcp", s.hostPort)
47+
lis, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", s.hostPort)
4848
if err != nil {
4949
s.logger.Bg().Fatal("Unable to create http listener", zap.Error(err))
5050
}

internal/config/tlscfg/flags_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ func TestFailedTLSFlags(t *testing.T) {
209209
case "server":
210210
exp := configoptional.Some(configtls.ServerConfig{})
211211
require.IsType(t, exp, result, "result should be of type *configtls.ServerConfig")
212+
default:
213+
t.Errorf("Unexpected side value: %s", metaTest.side)
212214
}
213215

214216
cmdLine[0] = "--prefix.tls.enabled=false"

internal/jptrace/sanitizer/emptyservicename.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ func sanitizeEmptyServiceName(traces ptrace.Traces) ptrace.Traces {
3434
needsModification = true
3535
case serviceName.Str() == "":
3636
needsModification = true
37+
default:
38+
// Service name is valid, no modification needed
3739
}
3840
if needsModification {
3941
break
@@ -63,6 +65,8 @@ func sanitizeEmptyServiceName(traces ptrace.Traces) ptrace.Traces {
6365
attributes.PutStr(string(otelsemconv.ServiceNameKey), serviceNameWrongType)
6466
case serviceName.Str() == "":
6567
attributes.PutStr(string(otelsemconv.ServiceNameKey), emptyServiceName)
68+
default:
69+
// Service name is valid, no action needed
6670
}
6771
}
6872

internal/sampling/samplingstrategy/adaptive/provider_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ func TestProviderRealisticRunCalculationLoop(t *testing.T) {
123123
case http.MethodDelete:
124124
assert.InEpsilon(t, 0.0005, s.ProbabilisticSampling.SamplingRate, 0.025,
125125
"Over sampled, halve probability")
126+
default:
127+
t.Errorf("Unexpected operation: %s", s.Operation)
126128
}
127129
}
128130
}

internal/storage/metricstore/elasticsearch/reader_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,8 @@ func TestGetLatencies_WithDifferentQuantiles(t *testing.T) {
624624
params.Quantile = 0.75
625625
case "0.95 quantile":
626626
params.Quantile = 0.95
627+
default:
628+
t.Errorf("Unexpected test case name: %s", tc.name)
627629
}
628630

629631
metricFamily, err := reader.GetLatencies(context.Background(), params)

internal/storage/metricstore/factory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ func (*Factory) getFactoryOfType(factoryType string) (storage.V1MetricStoreFacto
5959
return prometheus.NewFactory(), nil
6060
case disabledStorageType:
6161
return disabled.NewFactory(), nil
62+
default:
63+
return nil, fmt.Errorf("unknown metrics type %q. Valid types are %v", factoryType, AllStorageTypes)
6264
}
63-
return nil, fmt.Errorf("unknown metrics type %q. Valid types are %v", factoryType, AllStorageTypes)
6465
}
6566

6667
// Initialize implements storage.V1MetricStoreFactory.

0 commit comments

Comments
 (0)