template_errors.go (1609B)
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 "fmt"
18
19 "github.com/gohugoio/hugo/common/herrors"
20 "github.com/spf13/afero"
21 )
22
23 type templateInfo struct {
24 name string
25 template string
26 isText bool // HTML or plain text template.
27
28 // Used to create some error context in error situations
29 fs afero.Fs
30
31 // The filename relative to the fs above.
32 filename string
33
34 // The real filename (if possible). Used for logging.
35 realFilename string
36 }
37
38 func (t templateInfo) Name() string {
39 return t.name
40 }
41
42 func (t templateInfo) Filename() string {
43 return t.realFilename
44 }
45
46 func (t templateInfo) IsZero() bool {
47 return t.name == ""
48 }
49
50 func (t templateInfo) resolveType() templateType {
51 return resolveTemplateType(t.name)
52 }
53
54 func (info templateInfo) errWithFileContext(what string, err error) error {
55 err = fmt.Errorf(what+": %w", err)
56 fe := herrors.NewFileErrorFromName(err, info.realFilename)
57 f, err := info.fs.Open(info.filename)
58 if err != nil {
59 return err
60 }
61 defer f.Close()
62 return fe.UpdateContent(f, nil)
63
64 }