main 98594d0

context.go RawHistoryFolder

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	// Exception is the name of the .NET exception class when the error is an
25	// exception that the C# runner reports without a position, such as
26	// "System.ArgumentException"; Raw is then the message the exception was
27	// built with. The runner prints Exception.ToString(), which starts with the
28	// class and the message formatted by .NET (see [Error.Error]) and goes on
29	// with a stack trace that is not reproduced.
30	Exception string
31
32	// fileOnly makes the prefix the file name alone, for errors of the YAML
33	// parser, which the reference reports without a position.
34	fileOnly bool
35
36	cause error
37}
38
39// Unwrap returns the error of package expr behind an evaluation error, if any.
40func (e *Error) Unwrap() error { return e.cause }
41
42// Error returns the message with its prefix, as the reference does:
43// "file.yml (Line: 3, Col: 5): message".
44func (e *Error) Error() string {
45	if e.Exception != "" {
46		switch e.Exception {
47		case "System.ArgumentNullException":
48			return e.Exception + ": Value cannot be null. (Parameter '" + e.Raw + "')"
49		case "System.ArgumentOutOfRangeException":
50			return e.Exception + ": Specified argument was out of the range of valid values. (Parameter '" + e.Raw + "')"
51		}
52		return e.Exception + ": " + e.Raw
53	}
54	pos := e.Pos
55	if e.fileOnly {
56		pos = Pos{}
57	}
58	if p := errorPrefix(e.File, pos); p != "" {
59		return p + ": " + e.Raw
60	}
61	return e.Raw
62}
63
64func errorPrefix(file string, p Pos) string {
65	switch {
66	case file != "" && p.Line > 0:
67		return fmt.Sprintf("%s (Line: %d, Col: %d)", file, p.Line, p.Col)
68	case file != "":
69		return file
70	case p.Line > 0:
71		return fmt.Sprintf("(Line: %d, Col: %d)", p.Line, p.Col)
72	}
73	return ""
74}
75
76// message is an error whose text is one of the reference's messages, which
77// start with a capital letter.
78type message string
79
80func (m message) Error() string { return string(m) }
81
82// Context collects the errors of a read and carries its schema.
83type Context struct {
84	// MaxDepth bounds the nesting of sequences and mappings that [ReadYAML]
85	// accepts. A deeper document ends the read with "Maximum object depth
86	// exceeded", as the runner's TemplateMemory does. Zero means no limit.
87	MaxDepth int
88	// ErrorLimit is the number of errors kept; later ones are dropped. Zero
89	// means [MaxErrors]. The runner keeps ten when it loads an action.yml.
90	ErrorLimit int
91	// RunnerYAML makes the YAML reader as strict as the C# runner's when it
92	// loads an action.yml: an anchor is an error ("Anchors are not currently
93	// supported. Remove the anchor 'x'", and so is the alias that needs it),
94	// and so is every explicit tag but !!bool, !!float, !!int and !!null
95	// ("Unexpected tag 'tag:yaml.org,2002:str'": the runner knows no !!str).
96	// Such an error is reported alone, without the schema errors that the
97	// runner would have found in the part of the document before it.
98	// Mapping keys are resolved like values (~ is a null key, 0x10 is the key
99	// 16, TRUE is true), 0x and 0o integers wrap as int32 (0xFFFFFFFF is -1),
100	// and duplicate keys compare with .NET's OrdinalIgnoreCase
101	// (expr.OrdinalFold) instead of JavaScript's toUpperCase.
102	RunnerYAML bool
103
104	schema *Schema
105	errors []*Error
106	// Dropped counts the errors beyond the limit.
107	dropped int
108}
109
110// NewContext returns a context that validates against schema.
111func NewContext(schema *Schema) *Context { return &Context{schema: schema} }
112
113// Schema returns the schema the context validates against.
114func (c *Context) Schema() *Schema { return c.schema }
115
116// Errors returns the errors recorded so far.
117func (c *Context) Errors() []*Error { return append([]*Error(nil), c.errors...) }
118
119// HasErrors reports whether an error was recorded.
120func (c *Context) HasErrors() bool { return len(c.errors) > 0 }
121
122// Errorf records an error located at tok, which may be nil.
123func (c *Context) Errorf(tok Token, format string, a ...any) {
124	c.ErrorAt(tok, fmt.Sprintf(format, a...))
125}
126
127// ErrorAt records an error with message msg located at tok, which may be nil.
128func (c *Context) ErrorAt(tok Token, msg string) {
129	e := &Error{Raw: msg}
130	if tok != nil {
131		e.File, e.Pos = tok.File(), tok.Pos()
132	}
133	c.add(e)
134}
135
136// ErrorException records an exception the C# runner reports without a
137// position: class is the .NET class name ("System.ArgumentException") and msg
138// the message the exception was built with.
139func (c *Context) ErrorException(class, msg string) {
140	c.add(&Error{Raw: msg, Exception: class})
141}
142
143// ErrorInFile records an error that is about a whole file.
144func (c *Context) ErrorInFile(file, msg string) { c.add(&Error{File: file, Raw: msg}) }
145
146// ErrorAtPos records an error at an explicit position of file.
147func (c *Context) ErrorAtPos(file string, p Pos, msg string) {
148	c.add(&Error{File: file, Pos: p, Raw: msg})
149}
150
151func (c *Context) add(e *Error) {
152	limit := c.ErrorLimit
153	if limit <= 0 {
154		limit = MaxErrors
155	}
156	if len(c.errors) >= limit {
157		c.dropped++
158		return
159	}
160	c.errors = append(c.errors, e)
161}
162
163// Messages returns the full text of the errors.
164func (c *Context) Messages() []string {
165	out := make([]string, len(c.errors))
166	for i, e := range c.errors {
167		out[i] = e.Error()
168	}
169	return out
170}
171
172// Join returns the errors as one text, one per line.
173func (c *Context) Join() string { return strings.Join(c.Messages(), "\n") }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 3

↑↓ moveenter openesc close