From 2d5edc229b52027c9e37e67e40e480e7d737eb87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 22 Oct 2018 17:42:06 +0200 Subject: [PATCH] hugolib: Improve errors in /i18n handlling See #5324 --- common/herrors/file_error_test.go | 3 ++- common/herrors/line_number_extractors.go | 18 +++++++------ hugolib/site.go | 27 ++++++++++++++------ i18n/translationProvider.go | 32 +++++++++++++++++++++--- 4 files changed, 61 insertions(+), 19 deletions(-) diff --git a/common/herrors/file_error_test.go b/common/herrors/file_error_test.go index 0d4e82f6658..8b1674ba1cb 100644 --- a/common/herrors/file_error_test.go +++ b/common/herrors/file_error_test.go @@ -38,6 +38,7 @@ func TestToLineNumberError(t *testing.T) { {errors.New("parse failed: template: _default/bundle-resource-meta.html:11: unexpected in operand"), 0, 11, 1}, {errors.New(`failed:: template: _default/bundle-resource-meta.html:2:7: executing "main" at <.Titles>`), 0, 2, 7}, {errors.New("error in front matter: Near line 32 (last key parsed 'title')"), 0, 32, 1}, + {errors.New(`failed to load translations: (6, 7): was expecting token =, but got "g" instead`), 0, 6, 7}, } { got := ToFileErrorWithOffset("template", test.in, test.offset) @@ -46,7 +47,7 @@ func TestToLineNumberError(t *testing.T) { le, ok := got.(FileError) if test.lineNumber > 0 { - assert.True(ok) + assert.True(ok, errMsg) assert.Equal(test.lineNumber, le.LineNumber(), errMsg) assert.Equal(test.columnNumber, le.ColumnNumber(), errMsg) assert.Contains(got.Error(), strconv.Itoa(le.LineNumber())) diff --git a/common/herrors/line_number_extractors.go b/common/herrors/line_number_extractors.go index 8740afdf75e..93969b9676b 100644 --- a/common/herrors/line_number_extractors.go +++ b/common/herrors/line_number_extractors.go @@ -20,13 +20,17 @@ import ( var lineNumberExtractors = []lineNumberExtractor{ // Template/shortcode parse errors - newLineNumberErrHandlerFromRegexp("(.*?:)(\\d+)(:)(\\d+)?(.*)"), + newLineNumberErrHandlerFromRegexp(".*:(\\d+):(\\d*):"), + newLineNumberErrHandlerFromRegexp(".*:(\\d+):"), // TOML parse errors - newLineNumberErrHandlerFromRegexp("(.*Near line )(\\d+)(\\s.*)"), + newLineNumberErrHandlerFromRegexp(".*Near line (\\d+)(\\s.*)"), // YAML parse errors - newLineNumberErrHandlerFromRegexp("(line )(\\d+)(:)"), + newLineNumberErrHandlerFromRegexp("line (\\d+):"), + + // i18n bundle errors + newLineNumberErrHandlerFromRegexp("\\((\\d+),\\s(\\d*)"), } type lineNumberExtractor func(e error) (int, int) @@ -44,10 +48,10 @@ func extractLineNo(re *regexp.Regexp) lineNumberExtractor { col := 1 s := e.Error() m := re.FindStringSubmatch(s) - if len(m) >= 4 { - lno, _ := strconv.Atoi(m[2]) - if len(m) > 4 { - col, _ = strconv.Atoi(m[4]) + if len(m) >= 2 { + lno, _ := strconv.Atoi(m[1]) + if len(m) > 2 { + col, _ = strconv.Atoi(m[2]) } if col <= 0 { diff --git a/hugolib/site.go b/hugolib/site.go index e3f6d8f8cbc..127b143e53e 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -861,6 +861,24 @@ func (s *Site) loadData(fs afero.Fs) (err error) { return } +func (s *Site) errWithFileContext(err error, f source.File) error { + rfi, ok := f.FileInfo().(hugofs.RealFilenameInfo) + if !ok { + return err + } + + realFilename := rfi.RealFilename() + + err, _ = herrors.WithFileContextForFile( + err, + realFilename, + realFilename, + s.SourceSpec.Fs.Source, + herrors.SimpleLineMatcher) + + return err +} + func (s *Site) handleDataFile(r source.ReadableFile) error { var current map[string]interface{} @@ -888,14 +906,7 @@ func (s *Site) handleDataFile(r source.ReadableFile) error { data, err := s.readData(r) if err != nil { - realFilename := r.FileInfo().(hugofs.RealFilenameInfo).RealFilename() - err, _ = herrors.WithFileContextForFile( - _errors.Wrapf(err, "failed to read data file"), - realFilename, - realFilename, - s.SourceSpec.Fs.Source, - herrors.SimpleLineMatcher) - return err + return s.errWithFileContext(err, r) } if data == nil { diff --git a/i18n/translationProvider.go b/i18n/translationProvider.go index 4e937c5a19e..74e144007ef 100644 --- a/i18n/translationProvider.go +++ b/i18n/translationProvider.go @@ -16,8 +16,11 @@ package i18n import ( "errors" + "github.com/gohugoio/hugo/common/herrors" + "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/helpers" + "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/source" "github.com/nicksnyder/go-i18n/i18n/bundle" "github.com/nicksnyder/go-i18n/i18n/language" @@ -81,12 +84,12 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error { func addTranslationFile(bundle *bundle.Bundle, r source.ReadableFile) error { f, err := r.Open() if err != nil { - return _errors.Wrapf(err, "Failed to open translations file %q:", r.LogicalName()) + return _errors.Wrapf(err, "failed to open translations file %q:", r.LogicalName()) } - defer f.Close() err = bundle.ParseTranslationFileBytes(r.LogicalName(), helpers.ReaderToBytes(f)) + f.Close() if err != nil { - return _errors.Wrapf(err, "Failed to load translations in file %q:", r.LogicalName()) + return errWithFileContext(_errors.Wrapf(err, "failed to load translations"), r) } return nil } @@ -97,3 +100,26 @@ func (tp *TranslationProvider) Clone(d *deps.Deps) error { return nil } + +func errWithFileContext(inerr error, r source.ReadableFile) error { + rfi, ok := r.FileInfo().(hugofs.RealFilenameInfo) + if !ok { + return inerr + } + + realFilename := rfi.RealFilename() + f, err := r.Open() + if err != nil { + return inerr + } + defer f.Close() + + err, _ = herrors.WithFileContext( + inerr, + realFilename, + f, + herrors.SimpleLineMatcher) + + return err + +}