Skip to content

Commit

Permalink
Multiple error handler labels 2 (#462)
Browse files Browse the repository at this point in the history
* added second label to error_handler in test config

* fix: create one kindContents per error_handler block

* changelog entry
  • Loading branch information
johakoch authored Apr 4, 2022
1 parent d97e210 commit adb6162
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Unreleased changes are available as `avenga/couper:edge` container.

* **Fixed**
* Keys in object type attribute values are only handled case-insensitively if reasonable (e.g. they represent HTTP methods or header field values) ([#461](https://github.com/avenga/couper/pull/461))
* Multiple labels for [`error_handler` blocks](./docs/ERRORS.md#error_handler-specification) ([#462](https://github.com/avenga/couper/pull/462))

---

Expand Down
34 changes: 17 additions & 17 deletions config/configload/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/avenga/couper/errors"
)

type errorHandlerContent map[string]kindContent

type kindContent struct {
body hcl.Body
kinds []string
Expand All @@ -26,7 +24,7 @@ func configureErrorHandler(setter []collect.ErrorHandlerSetter, definedBackends
continue
}

ehc, err := newErrorHandlerContent(bodyToContent(body.HCLBody()))
kinds, ehc, err := newErrorHandlerContent(bodyToContent(body.HCLBody()))
if err != nil {
return err
}
Expand All @@ -42,10 +40,10 @@ func configureErrorHandler(setter []collect.ErrorHandlerSetter, definedBackends

if handler, has := ehs.(config.ErrorHandlerGetter); has {
defaultHandler := handler.DefaultErrorHandler()
_, exist := ehc[errors.Wildcard]
_, exist := kinds[errors.Wildcard]
if !exist {
for _, kind := range defaultHandler.Kinds {
_, exist = ehc[kind]
_, exist = kinds[kind]
if exist {
break
}
Expand All @@ -62,21 +60,22 @@ func configureErrorHandler(setter []collect.ErrorHandlerSetter, definedBackends

// newErrorHandlerContent reads given error_handler block contents and maps them by unique
// error kind declaration.
func newErrorHandlerContent(content *hcl.BodyContent) (errorHandlerContent, error) {
configuredKinds := make(errorHandlerContent)

func newErrorHandlerContent(content *hcl.BodyContent) (map[string]struct{}, []kindContent, error) {
if content == nil {
return configuredKinds, fmt.Errorf("empty hcl content")
return nil, nil, fmt.Errorf("empty hcl content")
}

configuredKinds := make(map[string]struct{})
var kindContents []kindContent

for _, block := range content.Blocks.OfType(errorHandler) {
kinds, err := newKindsFromLabels(block)
if err != nil {
return nil, err
return nil, nil, err
}
for _, k := range kinds {
if _, exist := configuredKinds[k]; exist {
return nil, hcl.Diagnostics{&hcl.Diagnostic{
return nil, nil, hcl.Diagnostics{&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("duplicate error type registration: %q", k),
Subject: &block.LabelRanges[0],
Expand All @@ -93,17 +92,18 @@ func newErrorHandlerContent(content *hcl.BodyContent) (errorHandlerContent, erro
Summary: fmt.Sprintf("error type is unknown: %q", k),
Subject: &subjRange,
}
return nil, hcl.Diagnostics{diag}
return nil, nil, hcl.Diagnostics{diag}
}

configuredKinds[k] = kindContent{
body: block.Body,
kinds: kinds,
}
configuredKinds[k] = struct{}{}
}
kindContents = append(kindContents, kindContent{
body: block.Body,
kinds: kinds,
})
}

return configuredKinds, nil
return configuredKinds, kindContents, nil
}

// newKindsFromLabels reads two possible kind formats and returns them per slice entry.
Expand Down
2 changes: 1 addition & 1 deletion server/testdata/integration/logs/01_couper.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ definitions {
jwt_regular = request.method
}

error_handler "jwt" {
error_handler "jwt_token_missing" "jwt" {
custom_log_fields = {
jwt_error = request.method
}
Expand Down

0 comments on commit adb6162

Please sign in to comment.