Skip to content

Commit 420a012

Browse files
mstoykovolegbespalovcodebien
authored
Fix options parsing error printing the most of the options (#4629)
The original idea of #4602 was to report at least the key with potentially the option in question. Due to the lack of support from the error - it can't parse it. The commit was trying to get everything between the previous and next new line. But in practice was getting anything until the end, both because of error in the coed to get the index, but also because there were no newlines due to how we marshal the data before that. This should fix both and make it less likely we just get everything until the end. Co-authored-by: Oleg Bespalov <oleg.bespalov@grafana.com> Co-authored-by: Ivan <2103732+codebien@users.noreply.github.com>
1 parent 8dbfc94 commit 420a012

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

internal/js/bundle.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (b *Bundle) populateExports(updateOptions bool, bi *BundleInstance) error {
208208
continue
209209
}
210210
var data []byte
211-
data, err = json.Marshal(v.Export())
211+
data, err = json.MarshalIndent(v.Export(), "", " ")
212212
if err != nil {
213213
err = fmt.Errorf("error parsing script options: %w", err)
214214
return
@@ -217,7 +217,8 @@ func (b *Bundle) populateExports(updateOptions bool, bi *BundleInstance) error {
217217
dec.DisallowUnknownFields()
218218
if err = dec.Decode(&b.Options); err != nil {
219219
if uerr := json.Unmarshal(data, &b.Options); uerr != nil {
220-
uerr = beautifyJSONUnmarshalError(data, uerr)
220+
// Beautify the error so we can try to show the user the key and potential value that is failing to parse
221+
uerr = beautifyOptionsJSONUnmarshalError(data, uerr)
221222
err = errext.WithAbortReasonIfNone(
222223
errext.WithExitCodeIfNone(uerr, exitcodes.InvalidConfig),
223224
errext.AbortedByScriptError,
@@ -248,12 +249,12 @@ func (b *Bundle) populateExports(updateOptions bool, bi *BundleInstance) error {
248249
return nil
249250
}
250251

251-
func beautifyJSONUnmarshalError(data []byte, err error) error {
252+
func beautifyOptionsJSONUnmarshalError(data []byte, err error) error {
252253
unmarshalTypError := new(json.UnmarshalTypeError)
253254
if errors.As(err, &unmarshalTypError) {
254255
e := unmarshalTypError
255256
previousNewLineIndex := max(bytes.LastIndexByte(data[:e.Offset], '\n'), 0)
256-
nextNewLineIndex := max(bytes.IndexByte(data[e.Offset:], '\n'), len(data)-1)
257+
nextNewLineIndex := max(min(bytes.IndexByte(data[e.Offset:], '\n'), len(data)-1), (int)(e.Offset))
257258

258259
info := strings.TrimSpace(string(data[previousNewLineIndex:nextNewLineIndex]))
259260
err = fmt.Errorf("parsing options from script got error while parsing %q: %w", info, e)

internal/js/bundle_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ func TestNewBundle(t *testing.T) {
236236
`json: cannot unmarshal array into Go value of type lib.Options`,
237237
},
238238
"Bad value": {
239-
`{"tags":["something"]}`,
240-
`parsing options from script got error while parsing "{\"tags\":[\"something\"]": ` +
239+
`{
240+
"duration": "5m",
241+
"tags":["something"],
242+
"vus": 5
243+
}`,
244+
`parsing options from script got error while parsing "\"tags\": [": ` +
241245
`json: cannot unmarshal array into Go struct field Options.tags of type map[string]string`,
242246
},
243247
"Function": {

0 commit comments

Comments
 (0)