Skip to content

Commit

Permalink
move diags check to errors.Error
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Koch committed Aug 16, 2022
1 parent 17098f2 commit c97aa5d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
3 changes: 0 additions & 3 deletions config/runtime/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ func NewBackend(ctx *hcl.EvalContext, body hcl.Body, log *logrus.Entry,

b, err = newBackend(ctx, body, log, conf, store)
if err != nil {
if _, isDiags := err.(hcl.Diagnostics); isDiags {
return nil, err
}
return nil, errors.Configuration.Label(name).With(err)
}

Expand Down
20 changes: 20 additions & 0 deletions errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"net/http"
"strings"

"github.com/hashicorp/hcl/v2"
)

type Error struct {
Expand Down Expand Up @@ -132,6 +134,10 @@ func (e *Error) Unwrap() error {

// LogError contains additional context which should be used for logging purposes only.
func (e *Error) LogError() string {
if diags := e.getDiags(); diags != nil {
return diags.Error()
}

msg := AppendMsg(e.synopsis, e.label, e.message)

if e.inner != nil {
Expand All @@ -147,6 +153,20 @@ func (e *Error) LogError() string {
return msg
}

func (e *Error) getDiags() hcl.Diagnostics {
if e.inner != nil {
if diags, ok := e.inner.(hcl.Diagnostics); ok {
return diags
}

if innr, ok := e.inner.(*Error); ok {
return innr.getDiags()
}
}

return nil;
}

// HTTPStatus returns the configured http status code this error should be served with.
func (e *Error) HTTPStatus() int {
if e.httpStatus == 0 {
Expand Down
41 changes: 22 additions & 19 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4233,10 +4233,10 @@ func TestFunction_to_number_errors(t *testing.T) {
}

for _, tc := range []testCase{
{"string", "/v1/to_number/string", `expression evaluation error: ` + wd + `/01_couper.hcl:62,23-28: Invalid function argument; Invalid value for "v" parameter: cannot convert "two" to number; given string must be a decimal representation of a number.`},
{"bool", "/v1/to_number/bool", `expression evaluation error: ` + wd + `/01_couper.hcl:70,23-27: Invalid function argument; Invalid value for "v" parameter: cannot convert bool to number.`},
{"tuple", "/v1/to_number/tuple", `expression evaluation error: ` + wd + `/01_couper.hcl:78,23-24: Invalid function argument; Invalid value for "v" parameter: cannot convert tuple to number.`},
{"object", "/v1/to_number/object", `expression evaluation error: ` + wd + `/01_couper.hcl:86,23-24: Invalid function argument; Invalid value for "v" parameter: cannot convert object to number.`},
{"string", "/v1/to_number/string", wd + `/01_couper.hcl:62,23-28: Invalid function argument; Invalid value for "v" parameter: cannot convert "two" to number; given string must be a decimal representation of a number.`},
{"bool", "/v1/to_number/bool", wd + `/01_couper.hcl:70,23-27: Invalid function argument; Invalid value for "v" parameter: cannot convert bool to number.`},
{"tuple", "/v1/to_number/tuple", wd + `/01_couper.hcl:78,23-24: Invalid function argument; Invalid value for "v" parameter: cannot convert tuple to number.`},
{"object", "/v1/to_number/object", wd + `/01_couper.hcl:86,23-24: Invalid function argument; Invalid value for "v" parameter: cannot convert object to number.`},
} {
t.Run(tc.path[1:], func(subT *testing.T) {
helper := test.New(subT)
Expand Down Expand Up @@ -4277,9 +4277,9 @@ func TestFunction_length_errors(t *testing.T) {
}

for _, tc := range []testCase{
{"object", "/v1/length/object", `expression evaluation error: ` + wd + `/01_couper.hcl:123,19-26: Error in function call; Call to function "length" failed: collection must be a list, a map or a tuple.`},
{"string", "/v1/length/string", `expression evaluation error: ` + wd + `/01_couper.hcl:131,19-26: Error in function call; Call to function "length" failed: collection must be a list, a map or a tuple.`},
{"null", "/v1/length/null", `expression evaluation error: ` + wd + `/01_couper.hcl:139,26-30: Invalid function argument; Invalid value for "collection" parameter: argument must not be null.`},
{"object", "/v1/length/object", wd + `/01_couper.hcl:123,19-26: Error in function call; Call to function "length" failed: collection must be a list, a map or a tuple.`},
{"string", "/v1/length/string", wd + `/01_couper.hcl:131,19-26: Error in function call; Call to function "length" failed: collection must be a list, a map or a tuple.`},
{"null", "/v1/length/null", wd + `/01_couper.hcl:139,26-30: Invalid function argument; Invalid value for "collection" parameter: argument must not be null.`},
} {
t.Run(tc.path[1:], func(subT *testing.T) {
helper := test.New(subT)
Expand Down Expand Up @@ -4320,7 +4320,7 @@ func TestFunction_lookup_errors(t *testing.T) {
}

for _, tc := range []testCase{
{"null inputMap", "/v1/lookup/inputMap-null", `expression evaluation error: ` + wd + `/01_couper.hcl:200,26-30: Invalid function argument; Invalid value for "inputMap" parameter: argument must not be null.`},
{"null inputMap", "/v1/lookup/inputMap-null", wd + `/01_couper.hcl:200,26-30: Invalid function argument; Invalid value for "inputMap" parameter: argument must not be null.`},
} {
t.Run(tc.path[1:], func(subT *testing.T) {
helper := test.New(subT)
Expand Down Expand Up @@ -5298,6 +5298,12 @@ func TestEndpoint_ResponseNilEvaluation(t *testing.T) {
func TestEndpoint_ConditionalEvaluationError(t *testing.T) {
client := newClient()

wd, werr := os.Getwd()
if werr != nil {
t.Fatal(werr)
}
wd = wd + "/testdata/integration/endpoint_eval"

shutdown, hook := newCouper("testdata/integration/endpoint_eval/20_couper.hcl", test.New(t))
defer shutdown()

Expand All @@ -5307,13 +5313,13 @@ func TestEndpoint_ConditionalEvaluationError(t *testing.T) {
}

for _, tc := range []testCase{
{"/conditional/null", "20_couper.hcl:281,16-20: Null condition; The condition value is null. Conditions must either be true or false."},
{"/conditional/string", "20_couper.hcl:287,16-21: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/number", "20_couper.hcl:293,16-17: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/tuple", "20_couper.hcl:299,16-18: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/object", "20_couper.hcl:305,16-18: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/string/expr", "20_couper.hcl:311,16-30: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/number/expr", "20_couper.hcl:317,16-26: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/null", wd+"/20_couper.hcl:281,16-20: Null condition; The condition value is null. Conditions must either be true or false."},
{"/conditional/string", wd+"/20_couper.hcl:287,16-21: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/number", wd+"/20_couper.hcl:293,16-17: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/tuple", wd+"/20_couper.hcl:299,16-18: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/object", wd+"/20_couper.hcl:305,16-18: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/string/expr", wd+"/20_couper.hcl:311,16-30: Incorrect condition type; The condition expression must be of type bool."},
{"/conditional/number/expr", wd+"/20_couper.hcl:317,16-26: Incorrect condition type; The condition expression must be of type bool."},
} {
t.Run(tc.path[1:], func(subT *testing.T) {
helper := test.New(subT)
Expand Down Expand Up @@ -5343,10 +5349,7 @@ func TestEndpoint_ConditionalEvaluationError(t *testing.T) {
time.Sleep(time.Millisecond * 100)
entry := hook.LastEntry()
if entry != nil && entry.Level == logrus.ErrorLevel {
if !strings.HasPrefix(entry.Message, "expression evaluation error: ") {
subT.Errorf("wrong error message,\nexp: %s\ngot: %s", tc.expMessage, entry.Message)
}
if !strings.HasSuffix(entry.Message, tc.expMessage) {
if entry.Message != tc.expMessage {
subT.Errorf("wrong error message,\nexp: %s\ngot: %s", tc.expMessage, entry.Message)
}
}
Expand Down

0 comments on commit c97aa5d

Please sign in to comment.