github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/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) Name() string {
    38  	return t.name
    39  }
    40  
    41  func (t templateInfo) Filename() string {
    42  	return t.realFilename
    43  }
    44  
    45  func (t templateInfo) IsZero() bool {
    46  	return t.name == ""
    47  }
    48  
    49  func (t templateInfo) resolveType() templateType {
    50  	return resolveTemplateType(t.name)
    51  }
    52  
    53  func (info templateInfo) errWithFileContext(what string, err error) error {
    54  	err = errors.Wrapf(err, what)
    55  
    56  	err, _ = herrors.WithFileContextForFile(
    57  		err,
    58  		info.realFilename,
    59  		info.filename,
    60  		info.fs,
    61  		herrors.SimpleLineMatcher)
    62  
    63  	return err
    64  }