From 34b13d5dfe6bf281dcd56c74b4e7123e6c980c71 Mon Sep 17 00:00:00 2001 From: Stephan Renatus Date: Tue, 19 Dec 2023 13:32:29 +0100 Subject: [PATCH] internal/presentation: cleanup location handling Signed-off-by: Stephan Renatus --- cmd/eval_test.go | 14 +++++------ internal/presentation/presentation.go | 35 +++++++++++---------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/cmd/eval_test.go b/cmd/eval_test.go index fffddeb78b..caaa233bba 100755 --- a/cmd/eval_test.go +++ b/cmd/eval_test.go @@ -111,17 +111,17 @@ q { t.Fatalf("expected message '%v', got '%v'", expectedMessage, msg) } - loc1, ok1 := output.Errors[0].Location.(map[string]interface{}) - if !ok1 { - t.Fatal("unexpected location type") + loc1 := output.Errors[0].Location + if loc1 == nil { + t.Fatal("unexpected nil location") } - loc2, ok2 := output.Errors[1].Location.(map[string]interface{}) - if !ok2 { - t.Fatal("unexpected location type") + loc2 := output.Errors[1].Location + if loc2 == nil { + t.Fatal("unexpected nil location") } - if loc1["row"] == loc2["row"] { + if loc1.Row == loc2.Row { t.Fatal("expected 2 distinct error occurrences in policy") } }) diff --git a/internal/presentation/presentation.go b/internal/presentation/presentation.go index b645c853bc..57b6b63dd7 100644 --- a/internal/presentation/presentation.go +++ b/internal/presentation/presentation.go @@ -142,19 +142,14 @@ func NewOutputErrors(err error) []OutputError { switch typedErr := err.(type) { case *ast.Error: - oe := OutputError{ - Code: typedErr.Code, - Message: typedErr.Message, - Details: typedErr.Details, - err: typedErr, - } + errs = []OutputError{{ + Code: typedErr.Code, + Message: typedErr.Message, + Details: typedErr.Details, + Location: typedErr.Location, + err: typedErr, + }} - // TODO(patrick-east): Why does the JSON marshaller marshal - // location as `null` when err.location == nil?! - if typedErr.Location != nil { - oe.Location = typedErr.Location - } - errs = []OutputError{oe} case *topdown.Error: errs = []OutputError{{ Code: typedErr.Code, @@ -184,11 +179,9 @@ func NewOutputErrors(err error) []OutputError { } } case loader.Errors: - { - for _, e := range typedErr { - if e != nil { - errs = append(errs, NewOutputErrors(e)...) - } + for _, e := range typedErr { + if e != nil { + errs = append(errs, NewOutputErrors(e)...) } } default: @@ -239,10 +232,10 @@ func (e OutputErrors) Error() string { // library errors so that the JSON output given by the // presentation package is consistent and parsable. type OutputError struct { - Message string `json:"message"` - Code string `json:"code,omitempty"` - Location interface{} `json:"location,omitempty"` - Details interface{} `json:"details,omitempty"` + Message string `json:"message"` + Code string `json:"code,omitempty"` + Location *ast.Location `json:"location,omitempty"` + Details interface{} `json:"details,omitempty"` err error }