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

internal/presentation: cleanup location handling #6498

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions cmd/eval_test.go
Expand Up @@ -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")
}
})
Expand Down
35 changes: 14 additions & 21 deletions internal/presentation/presentation.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}

Expand Down