Skip to content

Commit

Permalink
Merge pull request #247 from meshery/nithish/feat/validation_types
Browse files Browse the repository at this point in the history
[Refactor] Add `ErrorV2` struct
  • Loading branch information
humblenginr authored Jan 13, 2023
2 parents 9488a38 + 8109c17 commit dd05bc2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 16 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,24 @@ func New(code string, severity Severity, sdescription []string, ldescription []s
}
}

func NewV2(code string, severity Severity, sdescription []string, ldescription []string, probablecause []string, remedy []string, additionalInfo interface{}) *ErrorV2 {
return &ErrorV2{
Code: code,
Severity: severity,
ShortDescription: sdescription,
LongDescription: ldescription,
ProbableCause: probablecause,
SuggestedRemediation: remedy,
AdditionalInfo: additionalInfo,
}
}

func (e *Error) Error() string { return strings.Join(e.LongDescription[:], ".") }

func (e *Error) ErrorV2(additionalInfo interface{}) ErrorV2 {
return ErrorV2{Code: e.Code, Severity: e.Severity, ShortDescription: e.ShortDescription, LongDescription: e.LongDescription, ProbableCause: e.ProbableCause, SuggestedRemediation: e.SuggestedRemediation, AdditionalInfo: additionalInfo}
}

func GetCode(err error) string {
if obj := err.(*Error); obj != nil && obj.Code != " " {
return obj.Code
Expand Down
17 changes: 16 additions & 1 deletion errors/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ type (
ProbableCause []string
SuggestedRemediation []string
}
// Limitations of Error struct defined above:
// There are different types of Errors. Each type of error contains different information.
// Short and Long Descriptions accept only strings and so they cannot contain structured information.
// e.g. The Validation Error information (instancePath, badValue etc.) cannot be contained in Error struct (or not in a structured way that the clients can make use of it)
//
// ErrorV2 struct adds the ability to express all different types of errors.
// ErrorV2 is backwards compatible with Error struct
ErrorV2 struct {
Code string
Severity Severity
ShortDescription []string
LongDescription []string
ProbableCause []string
SuggestedRemediation []string
AdditionalInfo interface{}
}
)

type Severity int
Expand All @@ -19,7 +35,6 @@ const (
Alert // Immediate action needed
Critical // Critical condition—default level
Fatal // Fatal condition

)

var (
Expand Down

0 comments on commit dd05bc2

Please sign in to comment.