1// SPDX-License-Identifier: MIT
2
3package template
4
5import (
6 "fmt"
7 "strings"
8)
9
10// MaxErrors is the number of errors a [Context] keeps; later ones are
11// dropped. The reference keeps all of them, which lets a hostile file make the
12// list as long as the file.
13const MaxErrors = 1000
14
15// Error is a problem found while reading or evaluating a template.
16type Error struct {
17 // File is the name of the file the problem is in, or "".
18 File string
19 // Pos is where the problem is; the zero value means no position.
20 Pos Pos
21 // Raw is the message without the "file (Line: l, Col: c): " prefix.
22 Raw string
23
24 // fileOnly makes the prefix the file name alone, for errors of the YAML
25 // parser, which the reference reports without a position.
26 fileOnly bool
27
28 cause error
29}
30
31// Unwrap returns the error of package expr behind an evaluation error, if any.
32func (e *Error) Unwrap() error { return e.cause }
33
34// Error returns the message with its prefix, as the reference does:
35// "file.yml (Line: 3, Col: 5): message".
36func (e *Error) Error() string {
37 pos := e.Pos
38 if e.fileOnly {
39 pos = Pos{}
40 }
41 if p := errorPrefix(e.File, pos); p != "" {
42 return p + ": " + e.Raw
43 }
44 return e.Raw
45}
46
47func errorPrefix(file string, p Pos) string {
48 switch {
49 case file != "" && p.Line > 0:
50 return fmt.Sprintf("%s (Line: %d, Col: %d)", file, p.Line, p.Col)
51 case file != "":
52 return file
53 case p.Line > 0:
54 return fmt.Sprintf("(Line: %d, Col: %d)", p.Line, p.Col)
55 }
56 return ""
57}
58
59// message is an error whose text is one of the reference's messages, which
60// start with a capital letter.
61type message string
62
63func (m message) Error() string { return string(m) }
64
65// Context collects the errors of a read and carries its schema.
66type Context struct {
67 schema *Schema
68 errors []*Error
69 // Dropped counts the errors beyond [MaxErrors].
70 dropped int
71}
72
73// NewContext returns a context that validates against schema.
74func NewContext(schema *Schema) *Context { return &Context{schema: schema} }
75
76// Schema returns the schema the context validates against.
77func (c *Context) Schema() *Schema { return c.schema }
78
79// Errors returns the errors recorded so far.
80func (c *Context) Errors() []*Error { return append([]*Error(nil), c.errors...) }
81
82// HasErrors reports whether an error was recorded.
83func (c *Context) HasErrors() bool { return len(c.errors) > 0 }
84
85// Errorf records an error located at tok, which may be nil.
86func (c *Context) Errorf(tok Token, format string, a ...any) {
87 c.ErrorAt(tok, fmt.Sprintf(format, a...))
88}
89
90// ErrorAt records an error with message msg located at tok, which may be nil.
91func (c *Context) ErrorAt(tok Token, msg string) {
92 e := &Error{Raw: msg}
93 if tok != nil {
94 e.File, e.Pos = tok.File(), tok.Pos()
95 }
96 c.add(e)
97}
98
99// ErrorInFile records an error that is about a whole file.
100func (c *Context) ErrorInFile(file, msg string) { c.add(&Error{File: file, Raw: msg}) }
101
102// ErrorAtPos records an error at an explicit position of file.
103func (c *Context) ErrorAtPos(file string, p Pos, msg string) {
104 c.add(&Error{File: file, Pos: p, Raw: msg})
105}
106
107func (c *Context) add(e *Error) {
108 if len(c.errors) >= MaxErrors {
109 c.dropped++
110 return
111 }
112 c.errors = append(c.errors, e)
113}
114
115// Messages returns the full text of the errors.
116func (c *Context) Messages() []string {
117 out := make([]string, len(c.errors))
118 for i, e := range c.errors {
119 out[i] = e.Error()
120 }
121 return out
122}
123
124// Join returns the errors as one text, one per line.
125func (c *Context) Join() string { return strings.Join(c.Messages(), "\n") }