-
Notifications
You must be signed in to change notification settings - Fork 2k
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
add formatting for hcl parsing error messages #5972
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've never been so excited for error formatting! Looking good, just want to clear up my confusion on the comment about the err
var.
if diag.HasErrors() { | ||
return multierror.Append(errors.New("failed to parse config"), diag.Errs()...) | ||
tr.EmitEvent(structs.NewTaskEvent(structs.TaskFailedValidation).SetDriverError(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't tell where this err
comes from, is it the correct error to use here or should it be something from diagErrs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, good catch! I am supposed to pass in the multierror that gets created in the next line, but blindly copy-pastad and forgot to change it. This was part of an attempt to create more granular task events (eg. TaskFailedValidation for config parsing failures, and TaskDriverFailure for failures that were because of the driver), so I moved the event emission out of MAIN:
and into runDriver()
. I am not sure if that is too messy for a small change like this, so I can put the task events back the way they were...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fixed, but please give a double check
@@ -70,7 +70,7 @@ func (b *HCLParser) parse(t *testing.T, config, out interface{}) { | |||
decSpec, diags := hclspecutils.Convert(b.spec) | |||
require.Empty(t, diags) | |||
|
|||
ctyValue, diag := ParseHclInterface(config, decSpec, b.vars) | |||
ctyValue, diag, _ := ParseHclInterface(config, decSpec, b.vars) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use err
instead of _
here, and require.Nil
on the error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had one test suggestion.
Also, could you update the PR description with a before and after? Was hard to tell what this change ends up doing to error messages.
Thank you @preetapan, I also added a separate TestParseInvalid to test this behavior directly. Working on the description now... |
@preetapan @nickethier description is updated |
@jazzyfresh Could you look into the test failures they look to be related to your changes |
@nickethier yes.. I knew I would inevitably break something in https://travis-ci.org/hashicorp/nomad/jobs/560098109#L793
My guess is that changing the task emit events changed something... or it's something else, I'm still looking into it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! LGTM once lint/tests are passing!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. This feels much better already; thanks! I noticed a couple of formatting issues, but good otherwise.
func formattedDiagnosticErrors(diag hcl.Diagnostics) []error { | ||
var errs []error | ||
for _, d := range diag { | ||
if d.Summary == "Extraneous JSON object property" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a comment pointing to where this text originates, so future us and notice when it changes or if this is no longer needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Added the comment before the method
if diag.HasErrors() { | ||
return multierror.Append(errors.New("failed to parse config"), diag.Errs()...) | ||
parseErr := multierror.Append(errors.New("failed to parse config"), diagErrs...) | ||
tr.EmitEvent(structs.NewTaskEvent(structs.TaskFailedValidation).SetDriverError(parseErr)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that in the snippet that this returns a generic Validation of task failed
as the message. It's because TaskFailedValidation
expects ValidationError
[1] to be set instead of DriverError
.
Also, using the multierror.Append
generates very awkward messages, like:
2019-07-18T10:22:30+07:00 Failed Validation 2 error(s) occurred:
* failed to parse config
* Invalid label: No argument or block type is named "bad_key".
It'd be nice to format the errors ourselves and present something like the following
2019-07-18T10:22:30+07:00 Failed Validation failed to parse config; found the following:
* Invalid label: No argument or block type is named "bad_key".
[1]
Lines 374 to 379 in 87d716e
case api.TaskFailedValidation: | |
if event.ValidationError != "" { | |
desc = event.ValidationError | |
} else { | |
desc = "Validation of task failed" | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going to address better multierror formatting in another PR
helper/pluginutils/loader/init.go
Outdated
multierror.Append(&mErr, diag.Errs()...) | ||
return multierror.Prefix(&mErr, "failed parsing config:") | ||
multierror.Append(&mErr, diagErrs...) | ||
return multierror.Prefix(&mErr, "failed to parse config") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep the trailing colon here, as Prefix()
prefixes all elements rather than just once on top. I have a sample output in https://play.golang.org/p/sj0JcGRRVGH .
Though, I'd suggest avoiding multierror
formatting. I find it awkward and we can reuse the formatter from other callers.
@@ -4,6 +4,7 @@ import ( | |||
"context" | |||
"errors" | |||
"fmt" | |||
"github.com/hashicorp/go-multierror" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should appear in following grouping of imports, along with other non-stdlib imports. I'd suggest configuring your IDE to use goimports
on save instead of plain gofmt
.
errStr = fmt.Sprintf("%s\n* %s", errStr, err.Error()) | ||
} | ||
return errors.New(errStr) | ||
return multierror.Append(errors.New("failed to parse config"), diagErrs...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of all of these, I like this previous formatting the most :P.
I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions. |
Overview
Clean up error messages produced by hcl2 library
Behavior
Before (error messages had these weird numbers in them)
nomad agent -dev
logsnomad alloc status
commandAfter (removed weird numbers and make message about invalid labels, not JSON)
nomad agent -dev
logsnomad alloc status
commandImplementation
MAIN:
inrun()
)nomad alloc status
results when the event was TaskDriverFailure, so I might need to look into that more if we want to preserve that behavior