normalizers: Updated error handling within the JSON helper function.
This commit is contained in:
@@ -42,6 +42,10 @@ func DecodeJSONPayload[T any](in event.Event) (T, error) {
|
||||
// label should be short and specific, e.g. "openweather observation".
|
||||
// outSchema should be the canonical schema constant.
|
||||
// build should contain ONLY provider/domain mapping logic.
|
||||
//
|
||||
// Error policy:
|
||||
// - NormalizeJSON wraps ALL failures with consistent context: "<label> normalize: <stage>: ..."
|
||||
// - build() should return specific errors without repeating the label prefix.
|
||||
func NormalizeJSON[T any, P any](
|
||||
in event.Event,
|
||||
label string,
|
||||
@@ -55,8 +59,7 @@ func NormalizeJSON[T any, P any](
|
||||
|
||||
payload, effectiveAt, err := build(parsed)
|
||||
if err != nil {
|
||||
// build() should already include provider-specific context where appropriate.
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("%s normalize: build: %w", label, err)
|
||||
}
|
||||
|
||||
out, err := Finalize(in, outSchema, payload, effectiveAt)
|
||||
|
||||
@@ -48,7 +48,7 @@ func buildObservation(parsed nwsObservationResponse) (model.WeatherObservation,
|
||||
if s := strings.TrimSpace(parsed.Properties.Timestamp); s != "" {
|
||||
t, err := time.Parse(time.RFC3339, s)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, time.Time{}, fmt.Errorf("nws observation normalize: invalid timestamp %q: %w", s, err)
|
||||
return model.WeatherObservation{}, time.Time{}, fmt.Errorf("invalid timestamp %q: %w", s, err)
|
||||
}
|
||||
ts = t
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func buildObservation(parsed omResponse) (model.WeatherObservation, time.Time, e
|
||||
if s := strings.TrimSpace(parsed.Current.Time); s != "" {
|
||||
t, err := parseOpenMeteoTime(s, parsed.Timezone, parsed.UTCOffsetSeconds)
|
||||
if err != nil {
|
||||
return model.WeatherObservation{}, time.Time{}, fmt.Errorf("openmeteo observation normalize: parse time %q: %w", s, err)
|
||||
return model.WeatherObservation{}, time.Time{}, fmt.Errorf("parse time %q: %w", s, err)
|
||||
}
|
||||
ts = t.UTC()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user