From 69e4eea920fd4e62141a1ca5370d4dfc7e5278a6 Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Mon, 22 Nov 2021 13:28:18 +0100 Subject: [PATCH] interp: Better error if format/group is not found --- pkg/interp/funcs.go | 81 ------------------------------- pkg/interp/interp.go | 81 +++++++++++++++++++++++++++++++ pkg/interp/interp.jq | 7 ++- pkg/interp/testdata/decode.fqtest | 10 ++++ pkg/interp/testdata/inputs.fqtest | 2 +- pkg/interp/value.go | 4 +- 6 files changed, 100 insertions(+), 85 deletions(-) diff --git a/pkg/interp/funcs.go b/pkg/interp/funcs.go index 5ad18f83..843d63ac 100644 --- a/pkg/interp/funcs.go +++ b/pkg/interp/funcs.go @@ -10,7 +10,6 @@ import ( "fmt" "hash" "io" - "io/ioutil" "net/url" "github.com/wader/fq/pkg/bitio" @@ -154,86 +153,6 @@ func makeHashFn(fn func() (hash.Hash, error)) func(c interface{}, a []interface{ } } -func (i *Interp) _registry(c interface{}, a []interface{}) interface{} { - uniqueFormats := map[string]decode.Format{} - - groups := map[string]interface{}{} - formats := map[string]interface{}{} - - for fsName := range i.registry.Groups { - var group []interface{} - - for _, f := range i.registry.MustGroup(fsName) { - group = append(group, f.Name) - if _, ok := uniqueFormats[f.Name]; ok { - continue - } - uniqueFormats[f.Name] = f - } - - groups[fsName] = group - } - - for _, f := range uniqueFormats { - vf := map[string]interface{}{ - "name": f.Name, - "description": f.Description, - "probe_order": f.ProbeOrder, - "root_name": f.RootName, - "root_array": f.RootArray, - } - - var dependenciesVs []interface{} - for _, d := range f.Dependencies { - var dNamesVs []interface{} - for _, n := range d.Names { - dNamesVs = append(dNamesVs, n) - } - dependenciesVs = append(dependenciesVs, dNamesVs) - } - if len(dependenciesVs) > 0 { - vf["dependencies"] = dependenciesVs - } - var groupsVs []interface{} - for _, n := range f.Groups { - groupsVs = append(groupsVs, n) - } - if len(groupsVs) > 0 { - vf["groups"] = groupsVs - } - - if f.Files != nil { - files := map[string]interface{}{} - - entries, err := f.Files.ReadDir(".") - if err != nil { - return err - } - - for _, e := range entries { - f, err := f.Files.Open(e.Name()) - if err != nil { - return err - } - b, err := ioutil.ReadAll(f) - if err != nil { - return err - } - files[e.Name()] = string(b) - } - - vf["files"] = files - } - - formats[f.Name] = vf - } - - return map[string]interface{}{ - "groups": groups, - "formats": formats, - } -} - func (i *Interp) queryEscape(c interface{}, a []interface{}) interface{} { s, err := toString(c) if err != nil { diff --git a/pkg/interp/interp.go b/pkg/interp/interp.go index 3ce699e4..2906f6a8 100644 --- a/pkg/interp/interp.go +++ b/pkg/interp/interp.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "io/fs" + "io/ioutil" "math/big" "path/filepath" "strconv" @@ -607,6 +608,86 @@ func (i *Interp) makeStateFn(state *interface{}) func(c interface{}, a []interfa } } +func (i *Interp) _registry(c interface{}, a []interface{}) interface{} { + uniqueFormats := map[string]decode.Format{} + + groups := map[string]interface{}{} + formats := map[string]interface{}{} + + for fsName := range i.registry.Groups { + var group []interface{} + + for _, f := range i.registry.MustGroup(fsName) { + group = append(group, f.Name) + if _, ok := uniqueFormats[f.Name]; ok { + continue + } + uniqueFormats[f.Name] = f + } + + groups[fsName] = group + } + + for _, f := range uniqueFormats { + vf := map[string]interface{}{ + "name": f.Name, + "description": f.Description, + "probe_order": f.ProbeOrder, + "root_name": f.RootName, + "root_array": f.RootArray, + } + + var dependenciesVs []interface{} + for _, d := range f.Dependencies { + var dNamesVs []interface{} + for _, n := range d.Names { + dNamesVs = append(dNamesVs, n) + } + dependenciesVs = append(dependenciesVs, dNamesVs) + } + if len(dependenciesVs) > 0 { + vf["dependencies"] = dependenciesVs + } + var groupsVs []interface{} + for _, n := range f.Groups { + groupsVs = append(groupsVs, n) + } + if len(groupsVs) > 0 { + vf["groups"] = groupsVs + } + + if f.Files != nil { + files := map[string]interface{}{} + + entries, err := f.Files.ReadDir(".") + if err != nil { + return err + } + + for _, e := range entries { + f, err := f.Files.Open(e.Name()) + if err != nil { + return err + } + b, err := ioutil.ReadAll(f) + if err != nil { + return err + } + files[e.Name()] = string(b) + } + + vf["files"] = files + } + + formats[f.Name] = vf + } + + return map[string]interface{}{ + "groups": groups, + "formats": formats, + } +} + func (i *Interp) makeStdioFn(t Terminal) func(c interface{}, a []interface{}) gojq.Iter { return func(c interface{}, a []interface{}) gojq.Iter { if c == nil { diff --git a/pkg/interp/interp.jq b/pkg/interp/interp.jq index b6a032f3..6990cf16 100644 --- a/pkg/interp/interp.jq +++ b/pkg/interp/interp.jq @@ -51,7 +51,12 @@ def input: catch ( . as $err | _input_decode_errors(. += {($h): $err}) as $_ - | "\($h): failed to decode (\($opts.decode_format)), try -d FORMAT to force" + | [ "\($h): \($opts.decode_format)" + , if $err | type == "string" then ": \($err)" + # TODO: if not string assume decode itself failed for now + else ": failed to decode (try -d FORMAT)" + end + ] | join("") | (_error_str | _errorln) , _input($opts; f) ) diff --git a/pkg/interp/testdata/decode.fqtest b/pkg/interp/testdata/decode.fqtest index e58183c3..57a49ad2 100644 --- a/pkg/interp/testdata/decode.fqtest +++ b/pkg/interp/testdata/decode.fqtest @@ -69,3 +69,13 @@ $ fq -d raw 'png({force: true}) | d' /test.mp3 0x000| 00| .| safe_to_copy: false 0x010|00 0f 00 00 03 4c 61 76 66 35 38 2e 34 35 2e 31|.....Lavf58.45.1| unknown0: raw bits * |until 0x283.7 (end) (628) | | +$ fq -i -d aaa . /test.mp3 +empty> \ +empty> \ +exitcode: 4 +stderr: +error: /test.mp3: aaa: format group not found +$ fq -n '"aaa" | decode("aaa")' +exitcode: 5 +stderr: +error: format group not found diff --git a/pkg/interp/testdata/inputs.fqtest b/pkg/interp/testdata/inputs.fqtest index 67a6bf46..4d502803 100644 --- a/pkg/interp/testdata/inputs.fqtest +++ b/pkg/interp/testdata/inputs.fqtest @@ -76,7 +76,7 @@ null $ fq . /a exitcode: 4 stderr: -error: /a: failed to decode (probe), try -d FORMAT to force +error: /a: probe: failed to decode (try -d FORMAT) $ fq -i -d raw . /a /b /c raw, ...[3]> ._format "raw" diff --git a/pkg/interp/value.go b/pkg/interp/value.go index 06a2a9c7..e3037f42 100644 --- a/pkg/interp/value.go +++ b/pkg/interp/value.go @@ -111,11 +111,11 @@ func (i *Interp) _decode(c interface{}, a []interface{}) interface{} { formatName, err := toString(a[0]) if err != nil { - return fmt.Errorf("%s: %w", formatName, err) + return err } decodeFormat, err := i.registry.Group(formatName) if err != nil { - return fmt.Errorf("%s: %w", formatName, err) + return err } dv, _, err := decode.Decode(i.evalContext.ctx, bv.bb, decodeFormat,