Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple error handler labels 2 #462

Merged
merged 3 commits into from
Apr 4, 2022
Merged
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
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