github.com/gohugoio/hugo@v0.88.1/tpl/tplimpl/template_errors.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package tplimpl
    15  
    16  import (
    17  	"github.com/gohugoio/hugo/common/herrors"
    18  	"github.com/pkg/errors"
    19  	"github.com/spf13/afero"
    20  )
    21  
    22  type templateInfo struct {
    23  	name     string
    24  	template string
    25  	isText   bool // HTML or plain text template.
    26  
    27  	// Used to create some error context in error situations
    28  	fs afero.Fs
    29  
    30  	// The filename relative to the fs above.
    31  	filename string
    32  
    33  	// The real filename (if possible). Used for logging.
    34  	realFilename string
    35  }
    36  
    37  func (t templateInfo) IsZero() bool {
    38  	return t.name == ""
    39  }
    40  
    41  func (t templateInfo) resolveType() templateType {
    42  	return resolveTemplateType(t.name)
    43  }
    44  
    45  func (info templateInfo) errWithFileContext(what string, err error) error {
    46  	err = errors.Wrapf(err, what)
    47  
    48  	err, _ = herrors.WithFileContextForFile(
    49  		err,
    50  		info.realFilename,
    51  		info.filename,
    52  		info.fs,
    53  		herrors.SimpleLineMatcher)
    54  
    55  	return err
    56  }