Skip to content

Commit

Permalink
Add defined errors for conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJoiner committed Apr 25, 2024
1 parent 93cc109 commit 7d29d93
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
24 changes: 24 additions & 0 deletions pkg/vss/convert/errors.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions pkg/vss/convert/payloadv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/vss/convert/payloadv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
}

Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -83,15 +83,15 @@ 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
}

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
}
Expand Down

0 comments on commit 7d29d93

Please sign in to comment.