diff --git a/pkg/vss/convert/errors.go b/pkg/vss/convert/errors.go new file mode 100644 index 0000000..45f8f03 --- /dev/null +++ b/pkg/vss/convert/errors.go @@ -0,0 +1,24 @@ +package convert + +import "fmt" + +// VersionError is an error for unsupported specversion. +type VersionError struct { + Version string +} + +// Error returns the error message. +func (e VersionError) Error() string { + return fmt.Sprintf("unsupported specversion: %s", e.Version) +} + +// FieldNotFoundError is an error for missing fields. +type FieldNotFoundError struct { + Field string + Lookup string +} + +// Error returns the error message. +func (e FieldNotFoundError) Error() string { + return fmt.Sprintf("field not found: %s", e.Field) +} diff --git a/pkg/vss/convert/payloadv1.go b/pkg/vss/convert/payloadv1.go index 8cbb1bf..3340835 100644 --- a/pkg/vss/convert/payloadv1.go +++ b/pkg/vss/convert/payloadv1.go @@ -40,7 +40,7 @@ func SignalsFromV1Payload(ctx context.Context, tokenGetter TokenIDGetter, jsonDa func subjectFromV1Data(jsonData []byte) (string, error) { result := gjson.GetBytes(jsonData, "subject") if !result.Exists() { - return "", errors.New("subject field not found") + return "", FieldNotFoundError{Field: "subject", Lookup: "subject"} } sub, ok := result.Value().(string) if !ok { @@ -52,7 +52,7 @@ func subjectFromV1Data(jsonData []byte) (string, error) { func timestampFromV1Data(jsonData []byte) (time.Time, error) { result := gjson.GetBytes(jsonData, "time") if !result.Exists() { - return time.Time{}, errors.New("time field not found") + return time.Time{}, FieldNotFoundError{Field: "timestamp", Lookup: "time"} } timeStr, ok := result.Value().(string) diff --git a/pkg/vss/convert/payloadv2.go b/pkg/vss/convert/payloadv2.go index 008bd52..345446d 100644 --- a/pkg/vss/convert/payloadv2.go +++ b/pkg/vss/convert/payloadv2.go @@ -26,7 +26,7 @@ func SignalsFromPayload(ctx context.Context, tokenGetter TokenIDGetter, jsonData case specVersion == specV2: return SignalsFromV2Payload(jsonData) default: - return nil, errors.New("unsupported specversion") + return nil, VersionError{Version: specVersion} } } @@ -36,7 +36,7 @@ func SignalsFromV2Payload(jsonData []byte) ([]vss.Signal, error) { signals := gjson.GetBytes(jsonData, "data.vehicle.signals") if !signals.Exists() { - return nil, errors.New("signals field not found") + return nil, FieldNotFoundError{Field: "signals", Lookup: "data.vehicle.signals"} } if !signals.IsArray() { return nil, errors.New("signals field is not an array") @@ -71,7 +71,7 @@ func SignalsFromV2Payload(jsonData []byte) ([]vss.Signal, error) { func tokenIDFromV2Data(jsonData []byte) (uint32, error) { tokenID := gjson.GetBytes(jsonData, "vehicleTokenID") if !tokenID.Exists() { - return 0, errors.New("vehicleTokenID field not found") + return 0, FieldNotFoundError{Field: "tokenID", Lookup: "vehicleTokenID"} } id, ok := tokenID.Value().(float64) if !ok { @@ -83,7 +83,7 @@ func tokenIDFromV2Data(jsonData []byte) (uint32, error) { func timestampFromV2Data(sigResult gjson.Result) (time.Time, error) { timestamp := sigResult.Get("timestamp") if !timestamp.Exists() { - return time.Time{}, errors.New("timestamp field not found") + return time.Time{}, FieldNotFoundError{Field: "timestamp", Lookup: "timestamp"} } return time.UnixMilli(int64(timestamp.Uint())).UTC(), nil } @@ -91,7 +91,7 @@ func timestampFromV2Data(sigResult gjson.Result) (time.Time, error) { func signalNameFromV2Data(sigResult gjson.Result) (string, error) { signalName := sigResult.Get("name") if !signalName.Exists() { - return "", errors.New("signalName field not found") + return "", FieldNotFoundError{Field: "signalName", Lookup: "name"} } return signalName.String(), nil }