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".
|
// label should be short and specific, e.g. "openweather observation".
|
||||||
// outSchema should be the canonical schema constant.
|
// outSchema should be the canonical schema constant.
|
||||||
// build should contain ONLY provider/domain mapping logic.
|
// 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](
|
func NormalizeJSON[T any, P any](
|
||||||
in event.Event,
|
in event.Event,
|
||||||
label string,
|
label string,
|
||||||
@@ -55,8 +59,7 @@ func NormalizeJSON[T any, P any](
|
|||||||
|
|
||||||
payload, effectiveAt, err := build(parsed)
|
payload, effectiveAt, err := build(parsed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// build() should already include provider-specific context where appropriate.
|
return nil, fmt.Errorf("%s normalize: build: %w", label, err)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := Finalize(in, outSchema, payload, effectiveAt)
|
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 != "" {
|
if s := strings.TrimSpace(parsed.Properties.Timestamp); s != "" {
|
||||||
t, err := time.Parse(time.RFC3339, s)
|
t, err := time.Parse(time.RFC3339, s)
|
||||||
if err != nil {
|
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
|
ts = t
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func buildObservation(parsed omResponse) (model.WeatherObservation, time.Time, e
|
|||||||
if s := strings.TrimSpace(parsed.Current.Time); s != "" {
|
if s := strings.TrimSpace(parsed.Current.Time); s != "" {
|
||||||
t, err := parseOpenMeteoTime(s, parsed.Timezone, parsed.UTCOffsetSeconds)
|
t, err := parseOpenMeteoTime(s, parsed.Timezone, parsed.UTCOffsetSeconds)
|
||||||
if err != nil {
|
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()
|
ts = t.UTC()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user