shithub: hugo

Download patch

ref: 2bf686ee217808186385bfcf6156f15bbdb33651
parent: 9f74dc2a52b6f568b5a060b7a4be47196804b01f
author: Bjørn Erik Pedersen <[email protected]>
date: Mon Oct 22 13:42:06 EDT 2018

hugolib: Improve errors in /i18n handlling

See #5324

--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -620,7 +620,9 @@
 
 func (c *commandeer) handleBuildErr(err error, msg string) {
 	c.buildErr = err
-	c.logger.ERROR.Printf("%s: %s", msg, err)
+
+	c.logger.ERROR.Print(msg + ":\n\n")
+	c.logger.ERROR.Println(helpers.FirstUpper(err.Error()))
 	if !c.h.quiet && c.h.verbose {
 		herrors.PrintStackTrace(err)
 	}
--- a/common/herrors/file_error_test.go
+++ b/common/herrors/file_error_test.go
@@ -38,6 +38,7 @@
 		{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 @@
 		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()))
--- a/common/herrors/line_number_extractors.go
+++ b/common/herrors/line_number_extractors.go
@@ -20,13 +20,17 @@
 
 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 @@
 		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 {
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -861,6 +861,24 @@
 	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 @@
 
 	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 {
--- a/i18n/translationProvider.go
+++ b/i18n/translationProvider.go
@@ -16,8 +16,11 @@
 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 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
 }
@@ -96,4 +99,27 @@
 	d.Translate = tp.t.Func(d.Language.Lang)
 
 	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
+
 }