Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/rancher/lasso v0.0.0-20230629200414-8a54b32e6792
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
golang.org/x/sync v0.2.0
golang.org/x/text v0.11.0
golang.org/x/tools v0.8.0
Expand Down Expand Up @@ -71,7 +72,6 @@ require (
go.opentelemetry.io/otel/sdk v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
Expand Down
50 changes: 50 additions & 0 deletions pkg/data/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package data

type errorCode int

const (
errorInvalidData errorCode = iota + 1
errorInvalidField
errorFieldValueNotFound
)

type DataError struct {
message string
code errorCode
}

// Error returns the underlying error message, satisfying the error interface
func (d *DataError) Error() string {
return d.message
}

func newDataError(message string, code errorCode) *DataError {
return &DataError{
message: message,
code: code,
}
}

// IsInvalidDataError checks if a given error indicates that the provided data field was invalid.
func IsInvalidDataError(err error) bool {
return checkErrTypeAndCode(err, errorInvalidData)
}

// IsInvalidFieldError checks if a given error indicates that the provided field was invalid.
func IsInvalidFieldError(err error) bool {
return checkErrTypeAndCode(err, errorInvalidField)
}

// IsFieldValueNotFoundError checks if a given error indicates that the provided field was not found in the provided
// data.
func IsFieldValueNotFoundError(err error) bool {
return checkErrTypeAndCode(err, errorFieldValueNotFound)
}

func checkErrTypeAndCode(err error, code errorCode) bool {
dataErr, ok := err.(*DataError)
if !ok {
return false
}
return dataErr.code == code
}
Loading