Skip to content

Commit

Permalink
chore: handle flattened errors related to whole entities
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo committed Oct 11, 2023
1 parent 7cd5218 commit 0a33736
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/dataplane/sendconfig/error_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestParseFlatEntityErrors(t *testing.T) {
APIVersion: "v1",
UID: "e7e5c93e-4d56-4cc3-8f4f-ff1fcbe95eb2",
Problems: map[string]string{
"": "failed conditional validation given value of field protocol",
"service:67338dc2-31fd-47b6-85a9-9c11d347d090.httpbin.httpbin.80": "failed conditional validation given value of field protocol",
"path": "value must be null",
},
},
Expand Down
51 changes: 43 additions & 8 deletions internal/dataplane/sendconfig/inmemory_error_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,48 @@ type ConfigErrorFields struct{}

// FlatEntityError represents a single Kong entity with one or more invalid fields.
type FlatEntityError struct {
Name string `json:"entity_name,omitempty" yaml:"entity_name,omitempty"`
ID string `json:"entity_id,omitempty" yaml:"entity_id,omitempty"`
Tags []string `json:"entity_tags,omitempty" yaml:"entity_tags,omitempty"`
Errors []FlatFieldError `json:"errors,omitempty" yaml:"errors,omitempty"`
// Name is the name of the Kong entity.
Name string `json:"entity_name,omitempty" yaml:"entity_name,omitempty"`

// ID is the ID of the Kong entity.
ID string `json:"entity_id,omitempty" yaml:"entity_id,omitempty"`

// Tags are the tags of the Kong entity.
Tags []string `json:"entity_tags,omitempty" yaml:"entity_tags,omitempty"`

// Type is the type of the Kong entity.
Type string `json:"entity_type,omitempty" yaml:"entity_type,omitempty"`

// Errors are the errors associated with the Kong entity.
Errors []FlatError `json:"errors,omitempty" yaml:"errors,omitempty"`
}

// FlatFieldError represents an error for a single field within a Kong entity.
type FlatFieldError struct {
// FlatErrorType tells whether a FlatError is associated with a single field or a whole entity.
type FlatErrorType string

const (
// FlatErrorTypeField is an error associated with a single field.
FlatErrorTypeField FlatErrorType = "field"

// FlatErrorTypeEntity is an error associated with a whole entity.
FlatErrorTypeEntity FlatErrorType = "entity"
)

// FlatError represents an error for a single field within a Kong entity or a whole entity.
type FlatError struct {
// Field is the name of the entity's field that has an error.
// Optional: Field can be empty if the error is associated with the whole entity.
Field string `json:"field,omitempty" yaml:"field,omitempty"`
// Message is the error associated with Field for single-value fields.

// Message is the error associated with Field (for single-value fields) or with a whole entity when Type is "entity".
Message string `json:"message,omitempty" yaml:"message,omitempty"`

// Messages are the errors associated with Field for multi-value fields. The array index in Messages matches the
// array index in the input.
Messages []string `json:"messages,omitempty" yaml:"messages,omitempty"`

// Type tells whether the error is associated with a single field or a whole entity.
Type FlatErrorType `json:"type,omitempty" yaml:"type,omitempty"`
}

// parseFlatEntityErrors takes a Kong /config error response body and parses its "fields.flattened_errors" value
Expand Down Expand Up @@ -81,7 +109,14 @@ func parseFlatEntityErrors(body []byte, log logrus.FieldLogger) ([]ResourceError
continue
}
if len(p.Message) > 0 {
raw.Problems[p.Field] = p.Message
switch p.Type {
case FlatErrorTypeField:
// If the error is associated with a single field, store it in the map under the field name.
raw.Problems[p.Field] = p.Message
case FlatErrorTypeEntity:
// If the error is associated with a whole entity, store it in the map under the entity type and name.
raw.Problems[fmt.Sprintf("%s:%s", ee.Type, ee.Name)] = p.Message
}
}
if len(p.Messages) > 0 {
for i, message := range p.Messages {
Expand Down

0 comments on commit 0a33736

Please sign in to comment.