diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-10-21 12:20:21 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-10-22 20:46:14 +0200 |
commit | d1661b823af25c50d3bbe5366ea40a3cdd52e237 (patch) | |
tree | cd84d18229fb9c294ff1be56d7c0ce92a8f46761 /tpl | |
parent | 7930d2132a3c36c1aaca20f16f56978c84656b0a (diff) | |
download | hugo-d1661b823af25c50d3bbe5366ea40a3cdd52e237.tar.gz hugo-d1661b823af25c50d3bbe5366ea40a3cdd52e237.zip |
hugolib: Continue the file context/line number errors work
See #5324
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/data/data.go | 24 | ||||
-rw-r--r-- | tpl/data/data_test.go | 21 | ||||
-rw-r--r-- | tpl/template.go | 19 | ||||
-rw-r--r-- | tpl/tplimpl/template_errors.go | 4 |
4 files changed, 27 insertions, 41 deletions
diff --git a/tpl/data/data.go b/tpl/data/data.go index 3f87eda31..03fd27606 100644 --- a/tpl/data/data.go +++ b/tpl/data/data.go @@ -59,7 +59,7 @@ func (ns *Namespace) GetCSV(sep string, urlParts ...string) (d [][]string, err e var req *http.Request req, err = http.NewRequest("GET", url, nil) if err != nil { - return nil, _errors.Wrapf(err, "Failed to create request for getCSV for resource %s:", url) + return nil, _errors.Wrapf(err, "failed to create request for getCSV for resource %s", url) } req.Header.Add("Accept", "text/csv") @@ -68,28 +68,22 @@ func (ns *Namespace) GetCSV(sep string, urlParts ...string) (d [][]string, err e var c []byte c, err = ns.getResource(req) if err != nil { - ns.deps.Log.ERROR.Printf("Failed to read CSV resource %q: %s", url, err) - return nil, nil + return nil, _errors.Wrapf(err, "failed to read CSV resource %q", url) } if !bytes.Contains(c, []byte(sep)) { - ns.deps.Log.ERROR.Printf("Cannot find separator %s in CSV for %s", sep, url) - return nil, nil + return nil, _errors.Errorf("cannot find separator %s in CSV for %s", sep, url) } if d, err = parseCSV(c, sep); err != nil { - ns.deps.Log.WARN.Printf("Failed to parse CSV file %s: %s", url, err) + err = _errors.Wrapf(err, "failed to parse CSV file %s", url) + clearCacheSleep(i, url) continue } break } - if err != nil { - ns.deps.Log.ERROR.Printf("Failed to read CSV resource %q: %s", url, err) - return nil, nil - } - return } @@ -103,7 +97,7 @@ func (ns *Namespace) GetJSON(urlParts ...string) (v interface{}, err error) { var req *http.Request req, err = http.NewRequest("GET", url, nil) if err != nil { - return nil, _errors.Wrapf(err, "Failed to create request for getJSON resource %s:", url) + return nil, _errors.Wrapf(err, "Failed to create request for getJSON resource %s", url) } req.Header.Add("Accept", "application/json") @@ -111,10 +105,8 @@ func (ns *Namespace) GetJSON(urlParts ...string) (v interface{}, err error) { var c []byte c, err = ns.getResource(req) if err != nil { - ns.deps.Log.ERROR.Printf("Failed to get JSON resource %s: %s", url, err) - return nil, nil + return nil, _errors.Wrapf(err, "failed to get getJSON resource %q", url) } - err = json.Unmarshal(c, &v) if err != nil { ns.deps.Log.WARN.Printf("Cannot read JSON from resource %s: %s", url, err) @@ -127,7 +119,7 @@ func (ns *Namespace) GetJSON(urlParts ...string) (v interface{}, err error) { } if err != nil { - ns.deps.Log.ERROR.Printf("Failed to get JSON resource %s: %s", url, err) + return nil, _errors.Wrapf(err, "failed to get getJSON resource %q", url) return nil, nil } return diff --git a/tpl/data/data_test.go b/tpl/data/data_test.go index 9ef969244..7a0640e95 100644 --- a/tpl/data/data_test.go +++ b/tpl/data/data_test.go @@ -21,8 +21,6 @@ import ( "strings" "testing" - jww "github.com/spf13/jwalterweatherman" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -110,13 +108,13 @@ func TestGetCSV(t *testing.T) { // Get on with it got, err := ns.GetCSV(test.sep, test.url) - require.NoError(t, err, msg) - if _, ok := test.expect.(bool); ok { - require.Equal(t, 1, int(ns.deps.Log.ErrorCounter.Count())) + require.Error(t, err, msg) require.Nil(t, got) continue } + + require.NoError(t, err, msg) require.Equal(t, 0, int(ns.deps.Log.ErrorCounter.Count())) require.NotNil(t, got, msg) @@ -140,12 +138,12 @@ func TestGetJSON(t *testing.T) { { `http://malformed/`, `{gomeetup:["Sydney","San Francisco","Stockholm"]}`, - jww.LevelError, + false, }, { `http://nofound/404`, ``, - jww.LevelError, + false, }, // Locals { @@ -156,7 +154,7 @@ func TestGetJSON(t *testing.T) { { "fail/no-file", "", - jww.LevelError, + false, }, } { @@ -198,13 +196,6 @@ func TestGetJSON(t *testing.T) { continue } - if errLevel, ok := test.expect.(jww.Threshold); ok && errLevel >= jww.LevelError { - logCount := ns.deps.Log.ErrorCounter.Count() - require.True(t, logCount >= 1, fmt.Sprintf("got log count %d", logCount)) - continue - } - require.NoError(t, err, msg) - require.Equal(t, 0, int(ns.deps.Log.ErrorCounter.Count()), msg) require.NotNil(t, got, msg) diff --git a/tpl/template.go b/tpl/template.go index 68673a1fc..09710206e 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -145,15 +145,20 @@ func (t *TemplateAdapter) extractIdentifiers(line string) []string { } func (t *TemplateAdapter) addFileContext(name string, inerr error) error { + if strings.HasPrefix(t.Name(), "_internal") { + return inerr + } + f, realFilename, err := t.fileAndFilename(t.Name()) if err != nil { - return err + return inerr + } defer f.Close() master, hasMaster := t.NameBaseTemplateName[name] - ferr1 := errors.Wrapf(inerr, "execute of template %q failed", realFilename) + ferr := errors.Wrap(inerr, "execute of template failed") // Since this can be a composite of multiple template files (single.html + baseof.html etc.) // we potentially need to look in both -- and cannot rely on line number alone. @@ -174,9 +179,8 @@ func (t *TemplateAdapter) addFileContext(name string, inerr error) error { } return false } - // TODO(bep) 2errors text vs HTML - fe, ok := herrors.WithFileContext(ferr1, f, "go-html-template", lineMatcher) + fe, ok := herrors.WithFileContext(ferr, realFilename, f, lineMatcher) if ok || !hasMaster { return fe } @@ -188,12 +192,11 @@ func (t *TemplateAdapter) addFileContext(name string, inerr error) error { } defer f.Close() - ferr2 := errors.Wrapf(inerr, "execute of template %q failed", realFilename) - fe, ok = herrors.WithFileContext(ferr2, f, "go-html-template", lineMatcher) + fe, ok = herrors.WithFileContext(ferr, realFilename, f, lineMatcher) if !ok { // Return the most specific. - return ferr1 + return ferr } return fe @@ -206,7 +209,7 @@ func (t *TemplateAdapter) fileAndFilename(name string) (afero.File, string, erro fi, err := fs.Stat(filename) if err != nil { - return nil, "", errors.Wrapf(err, "failed to Stat %q", filename) + return nil, "", err } f, err := fs.Open(filename) if err != nil { diff --git a/tpl/tplimpl/template_errors.go b/tpl/tplimpl/template_errors.go index a422d77f1..63695c5f6 100644 --- a/tpl/tplimpl/template_errors.go +++ b/tpl/tplimpl/template_errors.go @@ -33,13 +33,13 @@ type templateInfo struct { } func (info templateInfo) errWithFileContext(what string, err error) error { - err = errors.Wrapf(err, "file %q: %s:", info.realFilename, what) + err = errors.Wrapf(err, what) err, _ = herrors.WithFileContextForFile( err, + info.realFilename, info.filename, info.fs, - "go-html-template", herrors.SimpleLineMatcher) return err |