-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy patherrors.go
422 lines (345 loc) · 11.2 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package booklit
import (
"bufio"
"bytes"
"errors"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/segmentio/textio"
"github.com/vito/booklit/ast"
"github.com/vito/booklit/errhtml"
)
var errorTmpl *template.Template
func init() {
errorTmpl = template.New("errors").Funcs(template.FuncMap{
"error": func(err error) (template.HTML, error) {
buf := new(bytes.Buffer)
var prettyErr PrettyError
if errors.As(err, &prettyErr) {
renderErr := prettyErr.PrettyHTML(buf)
if renderErr != nil {
return "", renderErr
}
return template.HTML(buf.String()), nil
}
return template.HTML(
`<pre class="raw-error">` +
template.HTMLEscapeString(err.Error()) +
`</pre>`,
), nil
},
"annotate": func(loc ErrorLocation) (template.HTML, error) {
buf := new(bytes.Buffer)
err := loc.AnnotatedHTML(buf)
if err != nil {
return "", err
}
return template.HTML(buf.String()), nil
},
})
for _, asset := range errhtml.AssetNames() {
info, err := errhtml.AssetInfo(asset)
if err != nil {
panic(err)
}
content := strings.TrimRight(string(errhtml.MustAsset(asset)), "\n")
_, err = errorTmpl.New(filepath.Base(info.Name())).Parse(content)
if err != nil {
panic(err)
}
}
}
// ErrorResponse writes the error response page.
//
// If err implements PrettyHTML it can render its own HTML template with
// additional troubleshooting.
func ErrorResponse(w http.ResponseWriter, err error) {
renderErr := errorTmpl.Lookup("page.tmpl").Execute(w, err)
if renderErr != nil {
fmt.Fprintf(w, "failed to render error page: %s", renderErr)
}
}
// PrettyError is an interface for providing friendly error messages.
type PrettyError interface {
error
// PrettyPrint is called by the booklit CLI to print an error message to
// stderr.
PrettyPrint(io.Writer)
// PrettyHTML is called by the error page template to render HTML within the
// error page.
PrettyHTML(io.Writer) error
}
// ParseError is returned when a Booklit document fails to parse.
type ParseError struct {
Err error
ErrorLocation
}
// Error returns a 'parse error' error message.
func (err ParseError) Error() string {
return fmt.Sprintf("parse error: %s", err.Err)
}
// PrettyPrint prints the error message followed by a snippet of the source
// location where the error occurred.
func (err ParseError) PrettyPrint(out io.Writer) {
fmt.Fprintln(out, err.Annotate("%s", err))
fmt.Fprintln(out)
err.AnnotateLocation(out)
}
// PrettyHTML renders an HTML template containing the error message followed by
// a snippet of the source location where the error occurred.
func (err ParseError) PrettyHTML(out io.Writer) error {
return errorTmpl.Lookup("parse-error.tmpl").Execute(out, err)
}
// UnknownTagError is returned when a reference is made to an unknown tag.
type UnknownTagError struct {
TagName string
SimilarTags []Tag
ErrorLocation
}
// Error returns an 'unknown tag' error message.
func (err UnknownTagError) Error() string {
return fmt.Sprintf("unknown tag '%s'", err.TagName)
}
// PrettyPrint prints the error message, a snippet of the source code where the
// error occurred, and suggests similar tags.
func (err UnknownTagError) PrettyPrint(out io.Writer) {
fmt.Fprintln(out, err.Annotate("%s", err))
fmt.Fprintln(out)
err.AnnotateLocation(out)
if len(err.SimilarTags) == 0 {
fmt.Fprintf(out, "I couldn't find any similar tags. :(\n")
} else {
fmt.Fprintf(out, "These tags seem similar:\n\n")
for _, tag := range err.SimilarTags {
fmt.Fprintf(out, "- %s\n", tag.Name)
}
fmt.Fprintf(out, "\nDid you mean one of these?\n")
}
}
// PrettyHTML renders an HTML template containing the error message, a snippet
// of the source code where the error occurred, and suggests similar tags.
func (err UnknownTagError) PrettyHTML(out io.Writer) error {
return errorTmpl.Lookup("unknown-tag.tmpl").Execute(out, err)
}
// AmbiguousReferenceError is returned when a referenced tag is defined in
// multiple places.
type AmbiguousReferenceError struct {
TagName string
DefinedLocations []ErrorLocation
ErrorLocation
}
// Error returns an 'ambiguous target for tag' error message.
func (err AmbiguousReferenceError) Error() string {
return fmt.Sprintf(
"ambiguous target for tag '%s'",
err.TagName,
)
}
// PrettyPrint prints the error message, a snippet of the source code where the
// error occurred, and snippets for the definition location of each tag that
// was found.
func (err AmbiguousReferenceError) PrettyPrint(out io.Writer) {
fmt.Fprintln(out, err.Annotate("%s", err))
fmt.Fprintln(out)
err.AnnotateLocation(out)
fmt.Fprintf(out, "The same tag was defined in the following locations:\n\n")
for _, loc := range err.DefinedLocations {
fmt.Fprintf(out, "- %s:\n", loc.FilePath)
loc.AnnotateLocation(textio.NewPrefixWriter(out, " "))
}
fmt.Fprintf(out, "Tags must be unique so I know where to link to!\n")
}
// PrettyHTML renders a HTML template containing the error message, a snippet
// of the source code where the error occurred, and snippets for the definition
// location of each tag that was found.
func (err AmbiguousReferenceError) PrettyHTML(out io.Writer) error {
return errorTmpl.Lookup("ambiguous-reference.tmpl").Execute(out, err)
}
// UndefinedFunctionError is returned when a Booklit document tries to call a
// function that is not defined by any plugin.
type UndefinedFunctionError struct {
Function string
ErrorLocation
}
// Error returns an 'undefined function' error message.
func (err UndefinedFunctionError) Error() string {
return fmt.Sprintf(
"undefined function \\%s",
err.Function,
)
}
// PrettyPrint prints the error message and a snippet of the source code where
// the error occurred.
func (err UndefinedFunctionError) PrettyPrint(out io.Writer) {
fmt.Fprintln(out, err.Annotate("%s", err))
fmt.Fprintln(out)
err.AnnotateLocation(out)
}
// PrettyHTML renders an HTML template containing the error message and a
// snippet of the source code where the error occurred.
func (err UndefinedFunctionError) PrettyHTML(out io.Writer) error {
return errorTmpl.Lookup("undefined-function.tmpl").Execute(out, err)
}
// FailedFunctionError is returned when a plugin function called by a Booklit
// document returns an error.
type FailedFunctionError struct {
Function string
Err error
ErrorLocation
}
// Error returns a 'function \... returned an error' message specifying the
// function name and the error it returned.
func (err FailedFunctionError) Error() string {
return fmt.Sprintf(
"function \\%s returned an error: %s",
err.Function,
err.Err,
)
}
// PrettyPrint prints the error message and a snippet of the source code where
// the error occurred.
//
// If the error returned by the function is a PrettyError, PrettyPrint is
// called and its output is indented.
//
// Otherwise, the error is printed normally.
func (err FailedFunctionError) PrettyPrint(out io.Writer) {
fmt.Fprintln(out, err.Annotate("function \\%s returned an error", err.Function))
fmt.Fprintln(out)
err.AnnotateLocation(out)
if prettyErr, ok := err.Err.(PrettyError); ok {
prettyErr.PrettyPrint(textio.NewPrefixWriter(out, " "))
} else {
fmt.Fprintf(out, "\x1b[33m%s\x1b[0m\n", err.Err)
}
}
// PrettyHTML renders an HTML template containing the error message followed by
// a snippet of the source location where the error occurred.
//
// If the error returned by the function is a PrettyError, PrettyHTML will be
// called within the template to embed the error recursively.
func (err FailedFunctionError) PrettyHTML(out io.Writer) error {
return errorTmpl.Lookup("function-error.tmpl").Execute(out, err)
}
// TitleTwiceError is returned when a section tries to set \title twice.
type TitleTwiceError struct {
TitleLocation ErrorLocation
ErrorLocation
}
// Error returns a 'cannot set title twice' message.
func (err TitleTwiceError) Error() string {
return "cannot set title twice"
}
// PrettyPrint prints the error message and a snippet of the source code where
// the error occurred.
//
// If the error returned by the function is a PrettyError, PrettyPrint is
// called and its output is indented.
//
// Otherwise, the error is printed normally.
func (err TitleTwiceError) PrettyPrint(out io.Writer) {
fmt.Fprintln(out, err.Annotate("%s", err))
fmt.Fprintln(out)
err.AnnotateLocation(out)
fmt.Fprintf(out, "The section's title was first set here:\n\n")
err.TitleLocation.AnnotateLocation(out)
fmt.Fprintln(out, "Maybe the second \\title should be in a \\section{...}?")
}
// PrettyHTML renders an HTML template containing the error message followed by
// a snippet of the source location where the error occurred.
//
// If the error returned by the function is a PrettyError, PrettyHTML will be
// called within the template to embed the error recursively.
func (err TitleTwiceError) PrettyHTML(out io.Writer) error {
return errorTmpl.Lookup("title-twice-error.tmpl").Execute(out, err)
}
// ErrorLocation is the source location in a Booklit document where an error
// occurred.
type ErrorLocation struct {
FilePath string
NodeLocation ast.Location
Length int
}
// Annotate prepends the source location to the given message.
func (loc ErrorLocation) Annotate(msg string, args ...interface{}) string {
if loc.NodeLocation.Line == 0 {
return fmt.Sprintf("%s: %s", loc.FilePath, fmt.Sprintf(msg, args...))
}
return fmt.Sprintf("%s:%d: %s", loc.FilePath, loc.NodeLocation.Line, fmt.Sprintf(msg, args...))
}
// AnnotateLocation writes a plaintext snippet of the location in the Booklit
// document.
func (loc ErrorLocation) AnnotateLocation(out io.Writer) {
if loc.NodeLocation.Line == 0 {
// location unavailable
return
}
line, err := loc.lineInQuestion()
if err != nil {
fmt.Fprintln(out, err)
return
}
prefix := fmt.Sprintf("% 4d| ", loc.NodeLocation.Line)
fmt.Fprintf(out, "%s%s\n", prefix, line)
pad := strings.Repeat(" ", len(prefix)+loc.NodeLocation.Col-1)
fmt.Fprintf(out, "%s\x1b[31m%s\x1b[0m\n", pad, strings.Repeat("^", loc.Length))
}
type annotationData struct {
FilePath string
EOF bool
Lineno string
Prefix, Annotated, Suffix string
}
// AnnotatedHTML renders a HTML snippet of the error location in the Booklit
// document.
func (loc ErrorLocation) AnnotatedHTML(out io.Writer) error {
if loc.NodeLocation.Line == 0 {
// location unavailable
return nil
}
line, err := loc.lineInQuestion()
if err != nil {
return err
}
data := annotationData{
FilePath: loc.FilePath,
Lineno: fmt.Sprintf("% 4d", loc.NodeLocation.Line),
}
if line == "" {
data.EOF = true
}
offset := loc.NodeLocation.Col - 1
if len(line) >= offset+loc.Length {
data.Prefix = line[0:offset]
data.Annotated = line[offset : offset+loc.Length]
data.Suffix = line[offset+loc.Length:]
}
return errorTmpl.Lookup("annotated-line.tmpl").Execute(out, data)
}
func (loc ErrorLocation) lineInQuestion() (string, error) {
file, err := os.Open(loc.FilePath)
if err != nil {
return "", err
}
defer file.Close()
buf := bufio.NewReader(file)
for i := 0; i < loc.NodeLocation.Line-1; i++ {
_, _, err := buf.ReadLine()
if err != nil {
return "", err
}
}
lineInQuestion, _, err := buf.ReadLine()
if err != nil {
if err == io.EOF {
return "", nil
}
return "", err
}
return string(lineInQuestion), nil
}